Reputation: 2701
I am using js to handle post request with multipart/form-data. When a post request is sent from client with multipart/form-data, it always returns bad request error. When I send request via postman, it gives me the error: "invalid multipart payload format when sending a multipart post request". Whichever call method I use, there is no action on server side.
{
method: "POST",
path: "/uploadFile",
config: {
payload: {
output: "stream",
parse: true,
allow: "multipart/form-data"
},
handler: (request, reply) => {
console.log("get into handler");
let data = request.payload;
if (data.file) {
let name = data.file.hapi.filename;
console.log(name);
} else {
console.log("no data");
}
}
}
}
The client side code is like this:
$http({
method: 'POST',
url: serviceURL
headers: {
'Content-Type': 'multipart/form-data; boundary=--xxxxSEPARATIONxxxx--'
},
data: formData
});
Upvotes: 0
Views: 2444
Reputation: 963
Looks like you are using angular framework for the client side. According to my experience, change 'Content-Type' to undefined in the headers, the browser will set the correct format for it. Also add transformRequest: angular.identity Reference this link.
Your code should look like this:
$http({
method: 'POST',
url: serviceURL
headers: {
'Content-Type': undefined
},
transformRequest: angular.identity,
data: formData
});
Hope this work for you.
Upvotes: 1