Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
117
rated 0 times [  123] [ 6]  / answers: 1 / hits: 21168  / 9 Years ago, sat, august 8, 2015, 12:00:00

I was trying to use Node.js supertest to test some REST API I had written. I need to send a request equivalent to the following CURL request:



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.



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.


More From » node.js

 Answers
39

Try removing .type('form') from your tests, because it will set application/x-www-form-urlencoded as Content-Type, instead of multipart/form-data.


[#65487] Thursday, August 6, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sabrina

Total Points: 92
Total Questions: 92
Total Answers: 85

Location: Palestine
Member since Thu, Feb 2, 2023
1 Year ago
;