Question

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(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
Was it helpful?

Solution

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.

OTHER TIPS

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'
    }
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top