开发者

Problems making nodejs client for yahoo placemaker api

开发者 https://www.devze.com 2023-02-16 23:59 出处:网络
I need to make a nodejs client application that can send a POST request to YAHOO Placemaker API. I spent 1 day on this with no success so far. I see the http packets in Wireshark and it doesn\'t compl

I need to make a nodejs client application that can send a POST request to YAHOO Placemaker API. I spent 1 day on this with no success so far. I see the http packets in Wireshark and it doesn't complain either.

I am using the following code:

var http = require('http');

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

var req = http.request开发者_StackOverflow社区(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();

I do the same in php and it works there. Any suggestions are appreciated. Similar problems occurred when I tried to run my own server on expressjs. Somehow the above code doesn't produce the correct HTTP request. But the above snippet is directly taken from NodeJs documentation

Please Help!!

I get the 400 HTTP response code saying neither documentURL nore documentContent not found!

  • Lalith


Consider using Mikeal's request library to simplify your life and avoid any strange problems:

npm install request

Also, stop into #node.js and ask questions for a quicker response. Make sure to report back with your findings.


Node.js http request does not generate the appropriate header Content-Type, so just add it manually. You don't need Express so far...

var options = {
    host: 'wherein.yahooapis.com',
    port: 80,
    method: 'POST',
    path: '/v1/document',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号