[英]Trying to post multipart/form-data with node.js supertest
I was trying to use Node.js supertest to test some REST API I had written. 我试图使用Node.js supertest来测试我编写的一些REST API。 I need to send a request equivalent to the following CURL request: 我需要发送一个等同于以下CURL请求的请求:
curl -X POST -F api_key=KEY -F image=@my_file http://localhost:3000/v1/upload
I tried the following, but I got Uncaught TypeError: first argument must be a string or Buffer
. 我尝试了以下内容,但是我得到了Uncaught TypeError: first argument must be a string or Buffer
。
request.post('/v1/upload')
.type('form')
.field('api_key', 'abcd')
.attach('image', 'some path')
.end(function(err, res) {
res.body.error.should.equal('Invalid username/api_key.');
done();
});
I also tried sending it as this: 我也试过发送它:
request.post('/v1/upload')
.type('form')
.field('api_key', 'abcd')
.attach('image', 'some path')
.end(function(err, res) {
res.body.error.should.equal('Invalid username/api_key.');
done();
});
but the server can only parse the file upload request and not the api_key
. 但是服务器只能解析文件上传请求而不能解析api_key
。
尝试从测试中删除 .type('form')
,因为它会将application/x-www-form-urlencoded
为Content-Type,而不是multipart/form-data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.