I am working on an app where I need to send an image blob as FormData alongside some other data sent as Json.
For this I'm making an http post request to the api with the following data:
const blob = new Blob(['some-image], {type: 'image/jpeg'});
const formData = new FormData();
formData.append("file", blob);
const data = {
name: 'John',
age: '24',
uploads: formData
};
For http post request I'm using fetch:
async postData() {
try {
const res = await fetch(endpoint, {
method : 'POST',
headers : {
'Content-Type': 'application/json'
},
body: JSON.stringify(data),
});
return res.status;
} catch (error) {
console.log(error)
}
}
I'm unable to get the file in the backend which just shows an empty object inside uploads['file']
. Why is that? Is it not possible to send formData as json with other data?