I am trying to make a multipart POST request to my API using superagent.
My Code:
superagent
.post(apiUrl + '/api/company/profile/edit')
.field("profileData", profileData)
.attach('company_logo', logoFile )
.set('Accept', 'application/json')
.end(function(err, res){
if(err){
dispatch(updateProfileStatusAction("error", res));
} else {
dispatch(updateProfileStatusAction("success", res));
}
});
The problem I am having is that profileData
is an object that is nested. When I get the request in the API I see the value of profileData
as the string [Object, Object]
When I look at the documentation for multipart request with superagent https://visionmedia.github.io/superagent/#multipart-requests it appears like the .field()
is meant to be just a key, value pair rather then an object. I then tried to use .send({profileData: profileData}) instead of field, but when I do that I get an error saying that .attach and .send can not be used together in the same request.
I think it should be enough to use JSON.stringify()
to convert the JS_Object to a JSON string.
superagent
.post(apiUrl + '/api/company/profile/edit')
.field("profileData", JSON.stringify(profileData))
.attach('company_logo', logoFile )
...