I have a project with next js. in this project I am using fetch for handling requests.
I wrote a wrapper for my requests as below
fetch(url, {
method: method.toUpperCase(),
headers: 'Content-Type': 'application/json',
...headers,
};,
cache: cache,
body: body
? JSON.stringify(body):null
});
it works good for all requests with header Content-Type:’application/JSON
But I have an issue now I have a form and in this form, I have a file input so in this case my headers must be multipart/form-data.
I am using formData API but since I used application/JSON as the default header My data will be posted with the header of application/JSON. so I changed explicitly my headers for this specific request to multipart/form-data and it created another issue because in this way the browser doesn’t set boundary for this header.
this is my request
const onSubmitHandler = (values) => {
const formData = new FormData();
formData.delete('captcha');
formData.append('jobTitle', careerData.title);
for (let value in values) {
formData.append(value, values[value]);
}
setCaptchaIsRequired(false);
const globalService = new GlobalService();
globalService
.postCareer(
formData,
{ "Content-Type":"multipart/form-data" }
)
.then((res) => {
//some codes
}).catch(e => console.log(e, 'ERRROR'));
}
};
I want to know how can do this and if this hasn’t any solutions is it Ok to set boundary my self or not
Thank’s all of you in advanced