简体   繁体   中英

How do I send an Object along with an attached File in a Multipart superagent request?

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 )
 ...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM