我试图使用 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
。
关于javascript - 尝试使用 node.js supertest 发布 multipart/form-data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31892295/