javascript 如何使用 node.js 中的请求以多部分 formData 发送对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29153534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send an object in multipart formData using request in node.js
提问by doremi
I'm trying to formulate a POST
using request, but I keep getting an error anytime I try and add the to
object to formData
.
我正在尝试制定POST
using request,但每次尝试将to
对象添加到formData
.
var fs = require('fs');
var request = require('request');
var file = './test/assets/test.pdf';
var opts = {
url: 'my_service',
method: 'POST',
auth: { user: 'username', password: 'password' },
json: true,
formData: {
front: fs.createReadStream(file),
to: {
name: 'joe bob',
address_1: '123 main st',
...
}
}
};
request(opts, function(err, resp, body) {
console.log(err, body);
});
Here is the error:
这是错误:
/sandbox/project/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
source.on('error', function() {});
^
TypeError: undefined is not a function
at Function.DelayedStream.create (/Users/me/sandbox/project/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at FormData.CombinedStream.append (/Users/me/sandbox/project/node_modules/request/node_modules/combined-stream/lib/combined_stream.js:43:37)
at FormData.append (/Users/me/sandbox/lproject/node_modules/request/node_modules/form-data/lib/form_data.js:43:3)
at appendFormValue (/Users/me/sandbox/project/node_modules/request/request.js:466:21)
at Request.init (/Users/me/sandbox/project/node_modules/request/request.js:477:11)
at new Request (/Users/me/sandbox/project/node_modules/request/request.js:264:8)
at request (/Users/me/sandbox/project/node_modules/request/index.js:50:10)
at Object.<anonymous> (/Users/me/sandbox/project/test.js:30:1)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
If I remove the to
object, everything works.
如果我删除to
对象,一切正常。
Why is this - what am I doing wrong?
这是为什么 - 我做错了什么?
回答by Jordan Shurmer
The formData
attribute doesn't handle objectspassed in as a value. see the documentation. A solution would be to use JSON.stringify
该formData
属性不处理作为值传入的对象。请参阅文档。一个解决方案是使用JSON.stringify
var fs = require('fs');
var request = require('request');
var file = './test/assets/test.pdf';
var toObj = {
name: 'joe bob',
address_1: '123 main st',
...
};
var opts = {
url: 'my_service',
method: 'POST',
auth: { user: 'username', password: 'password' },
json: true,
formData: {
front: fs.createReadStream(file),
to: JSON.stringify(toObj)
}
};
request(opts, function(err, resp, body) {
console.log(err, body);
});
note: It's actually the form-data package which supports only strings. Request uses form-data. Here's their usage docwhich mentions using "a string, a buffer and a file stream."
注意:它实际上是仅支持字符串的表单数据包。请求使用表单数据。这是他们的使用文档,其中提到了使用“字符串、缓冲区和文件流”。
回答by jessrcl
I had a similar problem using the request module where everything worked until I added a new line to 'formData'. The only thing that worked for me was to create a string that would be the JSON that would make up the POST body outside of the request and pass it in with 'body' instead of 'formData'.
我在使用 request 模块时遇到了类似的问题,在我向“formData”添加新行之前,一切正常。唯一对我有用的是创建一个字符串,该字符串将构成请求之外的 POST 正文,并使用“body”而不是“formData”传递它。
var postBody = "post body content";
request({
method: "POST",
uri: "my_service",
auth: { user: 'username', password: 'password' },
body: '{' + postBody + '}',
...
}).on("error", function(error){
...
};