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'
:
'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:
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. But there is nothing preventing you from passing Grouping groups
as a json string in form data.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.