Reputation: 3373
I'm trying to post an array of object with ChaiHttp like this:
agent.post('route/to/api')
.send( locations: [{lat: lat1, lon: lon1}, {lat: lat2, lon: lon2}])
.end (err, res) -> console.log err, res
It returns an error as below:
TypeError: first argument must be a string or Buffer at ClientRequest.OutgoingMessage.end (_http_outgoing.js:524:11) at Test.Request.end (node_modules/superagent/lib/node/index.js:1020:9) at node_modules/chai-http/lib/request.js:251:12 at Test.then (node_modules/chai-http/lib/request.js:250:21)
events.js:141 throw er; // Unhandled 'error' event ^
Error: incorrect header check at Zlib._handle.onerror (zlib.js:363:17)
I also tried to post like this, as we do with postman:
agent.post('route/to/api')
.field( 'locations[0].lat', xxx)
.field( 'locations[0].lan', xxx)
.field( 'locations[1].lat', xxx)
.field( 'locations[2].lat', xxx)
.then (res) -> console.log res
but payload.locations is received as an undefined.
Any idea how to post an array of objects via chai-http?
EDIT:
Here is my route and I think there's something wrong with stream payload:
method: 'POST'
path:
config:
handler: my_handler
payload:
output: 'stream'
Upvotes: 4
Views: 3241
Reputation: 61
Faced the same problem and my solution was not to use a JSON Object for the send method but rather a raw string:
chai.request(uri)
.post("/auth")
.set('content-type', 'application/x-www-form-urlencoded')
.send(`Login[Username]=${validUser1.username}`)
.send(`Login[Password]=${validUser1.password}`)
.send(`RememberMe=false`)
.end((err, res) => {
res.should.have.status(200);
// ...
});
Upvotes: 0
Reputation: 30855
Try to use .send({locations: [{lat: lat1, lon: lon1}, {lat: lat2, lon: lon2}]})
. Because .field('a', 'b')
not working.
body
as a form data
.put('/path/endpoint')
.type('form')
.send({foo: 'bar'})
// .field('foo' , 'bar')
.end(function(err, res) {}
// headers received, set by the plugin apparently
'accept-encoding': 'gzip, deflate',
'user-agent': 'node-superagent/2.3.0',
'content-type': 'application/x-www-form-urlencoded',
'content-length': '127',
body
as application/json
.put('/path/endpoint')
.set('content-type', 'application/json')
.send({foo: 'bar'})
// .field('foo' , 'bar')
.end(function(err, res) {}
// headers received, set by the plugin apparently
'accept-encoding': 'gzip, deflate',
'user-agent': 'node-superagent/2.3.0',
'content-type': 'application/json',
'content-length': '105',
Upvotes: 2
Reputation: 1789
I had the same problme here. It seems that simply the chai-http documentation is wrong. It sais:
// Send some Form Data
chai.request(app)
.post('/user/me')
.field('_method', 'put')
.field('password', '123')
.field('confirmPassword', '123')
Which does NOT work. This worked for me:
chai.request(app)
.post('/create')
.send({
title: 'Dummy title',
description: 'Dummy description'
})
.end(function(err, res) { ... }
Upvotes: 2