如何使用Chai Http发布对象数组 [英] How to post an array of objects with Chai Http
问题描述
我正在尝试使用 ChaiHttp 发布一个对象数组,如下所示:
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
它返回如下错误:
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 投掷者//未处理的错误"事件 ^
events.js:141 throw er; // Unhandled 'error' event ^
错误:Zlib._handle.onerror上的标头检查不正确 (zlib.js:363:17)
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
但是payload.locations是未定义的.
but payload.locations is received as an undefined.
有什么想法如何通过chai-http发布对象数组吗?
Any idea how to post an array of objects via chai-http?
这是我的路线,我认为流有效负载有问题:
Here is my route and I think there's something wrong with stream payload:
method: 'POST'
path:
config:
handler: my_handler
payload:
output: 'stream'
推荐答案
我在这里也遇到了同样的问题.看来,简单的chai-http文档是错误的.它说:
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) { ... }
这篇关于如何使用Chai Http发布对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!