为yahoo placemaker api制作nodejs客户端的问题

时间:2011-03-14 22:15:20

标签: node.js yahoo-maps

我需要创建一个可以向YAHOO Placemaker API发送POST请求的nodejs客户端应用程序。到目前为止,我花了一天时间没有成功。我在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 nore documentContent!

  • Lalith

2 个答案:

答案 0 :(得分:1)

考虑使用Mikeal's request library来简化您的生活并避免任何奇怪的问题:

npm install request

此外,请停留在#node.js并提出问题以便更快地回复。务必报告您的调查结果。

答案 1 :(得分: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'
    }
};