如何在多部分超级代理请求中发送对象和附加文件?
How do I send an Object along with an attached File in a Multipart superagent request?
我正在尝试使用 superagent 向我的 API 发出多部分 POST 请求。
我的代码:
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));
}
});
我遇到的问题是 profileData
是一个嵌套的对象。当我在 API 中收到请求时,我看到 profileData
的值是字符串 [Object, Object]
当我查看有关使用超级代理 https://visionmedia.github.io/superagent/#multipart-requests 的多部分请求的文档时,它似乎 .field()
只是一个键值对,而不是一个对象。然后我尝试使用 .send({profileData: profileData}) 而不是字段,但是当我这样做时,我收到一条错误消息,指出 .attach 和 .send 不能在同一请求中一起使用。
我认为使用 JSON.stringify()
将 JS_Object 转换为 JSON 字符串应该足够了。
superagent
.post(apiUrl + '/api/company/profile/edit')
.field("profileData", JSON.stringify(profileData))
.attach('company_logo', logoFile )
...
我正在尝试使用 superagent 向我的 API 发出多部分 POST 请求。
我的代码:
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));
}
});
我遇到的问题是 profileData
是一个嵌套的对象。当我在 API 中收到请求时,我看到 profileData
的值是字符串 [Object, Object]
当我查看有关使用超级代理 https://visionmedia.github.io/superagent/#multipart-requests 的多部分请求的文档时,它似乎 .field()
只是一个键值对,而不是一个对象。然后我尝试使用 .send({profileData: profileData}) 而不是字段,但是当我这样做时,我收到一条错误消息,指出 .attach 和 .send 不能在同一请求中一起使用。
我认为使用 JSON.stringify()
将 JS_Object 转换为 JSON 字符串应该足够了。
superagent
.post(apiUrl + '/api/company/profile/edit')
.field("profileData", JSON.stringify(profileData))
.attach('company_logo', logoFile )
...