0

我需要制作一个可以向 YAHOO Placemaker API 发送 POST 请求的 nodejs 客户端应用程序。到目前为止,我花了 1 天的时间没有成功。我在 Wireshark 中看到了 http 数据包,它也没有抱怨。

我正在使用以下代码:

var http = require('http');

var options = {
        host: 'wherein.yahooapis.com',
        port: 80,
        path: '/v1/document',
        method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
//  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});
// write data to request body
//req.end('documentURL=http://www.usfca.edu&documentType=text/html&outputType=xml&appid=MrIhH33V34GNOpw91rqJuijGeiLQ7l4hhlJXXt3fOTS0.jAUY8kqhu6SxMhy7J90OSWElw--');
req.write('documentURL%3Dhttp%3A//www.usfca.edu%26documentType%3Dtext/html%26outputType%3Dxml%26appid%3DMrIhH33V34GNOpw91rqJuijGeiLQ7l4hhlJXXt3fOTS0.jAUY8kqhu6SxMhy7J90OSWElw--');
req.end();

我在 php 中做同样的事情,它在那里工作。任何建议表示赞赏。当我尝试在 expressjs 上运行我自己的服务器时,也出现了类似的问题。不知何故,上面的代码没有产生正确的 HTTP 请求。但是上面的代码片段直接取自 NodeJs 文档

请帮忙!!

我收到 400 HTTP 响应代码,说既没有找到 documentURL,也没有找到 documentContent!

  • 拉利斯
4

2 回答 2

1

Node.js http 请求不会生成相应的标头 Content-Type,所以只需手动添加即可。到目前为止,您不需要 Express...

var options = {
    host: 'wherein.yahooapis.com',
    port: 80,
    method: 'POST',
    path: '/v1/document',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
};
于 2012-01-21T08:23:14.227 回答
1

考虑使用Mikeal 的请求库来简化您的生活并避免任何奇怪的问题:

npm install request

此外,请停下来#node.js并提出问题以获得更快的响应。请务必报告您的发现。

于 2011-03-30T04:59:41.767 回答