我需要制作一个Nodejs客户端应用程序,该应用程序可以将邮政请求发送到Yahoo Placemaker API。到目前为止,我花了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响应代码,说找不到docorduturl nore docundentcontent!

  • 拉利斯
有帮助吗?

解决方案

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

npm install request

另外,请停止 #node.js 并提出问题以提高回应。确保报告您的发现。

其他提示

Node.js HTTP请求不会生成适当的标头内容类型,因此只需手动添加即可。到目前为止,您不需要Express ...

var options = {
    host: 'wherein.yahooapis.com',
    port: 80,
    method: 'POST',
    path: '/v1/document',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
};
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top