发送包含已编码JSON的POST请求

时间:2013-10-24 19:59:49

标签: node.js post

我正在尝试进行包含JSON编码表单的POST调用。

为什么我这样做?我别无选择,我正在使用Facebook API,它希望接收JSON编码数据并在接收JSON时触发错误。

我在执行以下操作时收到错误TypeError: stringify expects an object

var datas = JSON.stringify({ some: "JSON" });
request.post('https://graph.facebook.com/...', { form: datas }, function(error,        response, body) {
    //Fail before the callback call
});

如何避免?

1 个答案:

答案 0 :(得分:4)

第一行中的JSON.stringify不是在这里失败的,它是form属性,它应该是一个对象。

不要尝试将其作为表单数据发送,只需将JSON文本放在请求正文中即可。

var datas = JSON.stringify({ some: "JSON" });
request.post('https://graph.facebook.com/...', { body: datas }, function(error,        response, body) {
  //Fail before the callback call
});
相关问题