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

我试图使用Node.js supertest来testing我写的一些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(); }); 

但服务器只能parsingfile upload请求,而不能parsingapi_key

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