我正在尝试使用 ChaiHttp 发布一个对象数组像这样:
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 throw er; // Unhandled 'error' event ^
Error: incorrect header check at Zlib._handle.onerror (zlib.js:363:17)
我也试过这样发帖,就像我们用 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 被接收为未定义的。
知道如何通过 chai-http 发布对象数组吗?
编辑:
这是我的路线,我认为流负载有问题:
method: 'POST'
path:
config:
handler: my_handler
payload:
output: 'stream'
最佳答案
我在这里遇到了同样的问题。似乎只是 chai-http 文档是错误的。它说:
// Send some Form Data
chai.request(app)
.post('/user/me')
.field('_method', 'put')
.field('password', '123')
.field('confirmPassword', '123')
这是行不通的。这对我有用:
chai.request(app)
.post('/create')
.send({
title: 'Dummy title',
description: 'Dummy description'
})
.end(function(err, res) { ... }
关于node.js - 如何使用 Chai Http 发布对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36530334/