【问题标题】:How to post an array of objects with Chai Http如何使用 Chai Http 发布对象数组
【发布时间】:2016-07-31 12:45:11
【问题描述】:

我正在尝试使用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 投掷者; // 未处理的“错误”事件 ^

错误:Zlib._handle.onerror 中的标头检查不正确 (zlib.js:363:17)

我也尝试过这样发帖,就像我们对邮递员所做的那样:

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'

【问题讨论】:

  • @Cuthbert,没有。 field 方法接受字符串作为第二个参数。我改用JSON.stringify 并发表了我的帖子。但是我发现主要问题是超级代理或有效负载输出。

标签: node.js post chai hapijs


【解决方案1】:

我在这里遇到了同样的问题。似乎只是 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) { ... }

【讨论】:

  • 这如何回答这个问题?
  • 这不能回答问题,甚至不是正确的做法。如果您通过 multipart/formdata 发送文件怎么办?
【解决方案2】:

尝试使用.send({locations: [{lat: lat1, lon: lon1}, {lat: lat2, lon: lon2}]})。因为.field('a', 'b') 不工作。

  1. body作为表单数据

    .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',
    
  2. body 为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',
    

【讨论】:

    【解决方案3】:

    遇到同样的问题,我的解决方案不是使用 JSON 对象作为 send 方法,而是使用原始字符串:

        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);
                // ...
            });
    

    【讨论】:

      猜你喜欢
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-27
      • 1970-01-01
      • 2016-06-04
      • 2012-06-22
      • 2023-03-26
      相关资源
      最近更新 更多