2015-08-08 25 views
4

我試圖使用Node.js supertest來測試我寫的一些REST API。我需要一個請求,相當於發送到以下CURL請求:試圖發佈multipart/form-data與node.js supertest

curl -X POST -F api_key=KEY -F [email protected]_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

回答

6

嘗試去除你的測試.type('form'),因爲它將設置application/x-www-form-urlencoded的內容類型,而不是multipart/form-data