【问题标题】:Post array/object with the Request module使用请求模块发布数组/对象
【发布时间】:2012-09-12 02:08:54
【问题描述】:

我正在尝试使用 request 模块发出 http 发布请求

text = 'some data';

request.post('http://example.com/read', {form:{data: text}});

这适用于简单的字符串,但我需要能够发送数组或对象。

当我尝试在后处理程序中读取数组/对象时,数据属性为空。

这是怎么回事?非常感谢您的帮助。

【问题讨论】:

  • 发布您的服务器端代码。您是否正在使用 connect bodyParser 中间件?如果是这样,您希望 req.body.form.data 获取您的文本。
  • 是的,我正在使用 connect bodyParser 中间件。我只是在做 console.log(req.body) 这给了我空对象但是 req.body.form.data 我得到'属性数据未定义。

标签: arrays node.js object post request


【解决方案1】:

试试这个:

var request = require('request');

request({
  method: 'POST',
  uri: 'http://example.com/read',
  body: {'msg': 'secret'},
  json: true
}, function (error, response, body) {
  console.log('code: '+ response.statusCode);
  console.log(body);
})

让我知道它是否有效。

【讨论】:

  • 是的。完美的。反正我从来不想用form,只是觉得有必要。谢谢
猜你喜欢
  • 1970-01-01
  • 2018-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-19
  • 1970-01-01
  • 2020-06-06
  • 1970-01-01
相关资源
最近更新 更多