[英]Two different content types on one call
I'm posting to an api an object with two different params, each param needs a different 'Content-Type'
, the first param- groups
should have the following 'Content-Type'
: 我将具有两个不同参数的对象发布到api,每个参数需要一个不同的
'Content-Type'
,第一个参数groups
应具有以下'Content-Type'
:
'Content-Type': 'application/json',
And the second one: 第二个:
'Content-Type': 'multipart/form-data'
Is that possible to set a header with two different content types for the same post? 是否可以为同一帖子设置具有两种不同内容类型的标题?
const { result } = apiPost(`api/emails/attachments?request=${request}&ticketId=${ticketId}`, data, {
headers: { 'Content-Type': 'multipart/form-data' },
});
};
Ticket object: 票证对象:
public class ticket
{
[FromBodyAttribute] public Grouping groups { get; set; }
public IEnumerable<IFormFile> files { get; set; }
}
ApiPost: ApiPost:
export const apiFetch: <T>(url: string, options?: object) => Promise<T> = (
url: string,
options: object,
) => adalFetch(authContext, adalConfig.endpoints.api, axios, url, options);
export const apiPost = async <T>(url: string, data: object, headers: object): Promise<T> => {
const options = {
method: 'post',
data,
config: {
headers: headers || {
'Content-Type': 'application/json',
},
},
};
console.log(data);
console.log(options);
return apiFetch(url, options);
};
No, it's not possible, even if you add two Content-Type headers, the content body can only be of one type. 不,即使添加两个Content-Type标头,内容主体也只能是一种类型。 But there is nothing preventing you from passing
Grouping groups
as a json string in form data. 但是没有什么阻止您将
Grouping groups
作为json字符串传递到表单数据中。
const data = new FormData();
data.append('files', /* all your files */)
data.append('groups', JSON.stringify(groupsObject))
apiFetch(url, { method: 'post', data })
public class Ticket
{
public string groups { get; set; } // <-- Try 'Grouping' as well, may work
public IEnumerable<IFormFile> files { get; set; }
}
public IActionResult Action(Ticket ticket) {
var groups = JsonConvert.DeserializeObject<Grouping>(ticket.groups);
}
As far as I know, model binding does not deserialize json strings inside a form data binding, you will have to do it explicitly in your action method as shown above or by using a custom model binder. 据我所知,模型绑定不会在表单数据绑定内反序列化json字符串,您将必须在上述操作方法中或使用自定义模型绑定器来显式地执行它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.