繁体   English   中英

尝试使用node.js supertest发布multipart / form-data

[英]Trying to post multipart/form-data with node.js supertest

我试图使用Node.js supertest来测试我编写的一些REST API。 我需要发送一个等同于以下CURL请求的请求:

curl -X POST -F api_key=KEY -F image=@my_file http://localhost:3000/v1/upload

我尝试了以下内容,但是我得到了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();
});

我也试过发送它:

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();
});

但是服务器只能解析文件上传请求而不能解析api_key

尝试从测试中删除 .type('form') ,因为它会将application/x-www-form-urlencoded为Content-Type,而不是multipart/form-data

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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