How to post an array of objects with Chai Http
我正在尝试使用ChaiHttp发布对象数组,如下所示:
1 2 3 | agent.post('route/to/api') .send( locations: [{lat: lat1, lon: lon1}, {lat: lat2, lon: lon2}]) .end (err, res) -> console.log err, res |
它返回如下错误:
1
2
3
4
5 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)
我也尝试像邮递员一样张贴这样的文章:
1 2 3 4 5 6 | 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 |
但是payload.locations被接收为未定义。
任何想法如何通过chai-http发布对象数组?
编辑:
这是我的路线,我认为流有效负载有问题:
1 2 3 4 5 6 | method: 'POST' path: config: handler: my_handler payload: output: 'stream' |
我在这里也有同样的问题。 看来,简单的chai-http文档是错误的。 它说:
1 2 3 4 5 6 | // Send some Form Data chai.request(app) .post('/user/me') .field('_method', 'put') .field('password', '123') .field('confirmPassword', '123') |
这不起作用。 这为我工作:
1 2 3 4 5 6 7 | chai.request(app) .post('/create') .send({ title: 'Dummy title', description: 'Dummy description' }) .end(function(err, res) { ... } |
尝试使用
1 2 3 4 5 6 7 8 9 10 11 | .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', |
1 2 3 4 5 6 7 8 9 10 11 | .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', |
面对同样的问题,我的解决方案不是将JSON对象用于send方法,而是使用原始字符串:
1 2 3 4 5 6 7 8 9 10 | 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); // ... }); |