0

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
Lalith
  • 19,396
  • 17
  • 44
  • 54
  • why are you posting comments to your own question? If you think that your question is not clear enough don't hesitate to update it and include any relevant information which might help people willing to answer it. – Darin Dimitrov Mar 14 '11 at 22:18

2 Answers2

1

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.

DTrejo
  • 1,309
  • 9
  • 16
1

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'
    }
};
Ulflander
  • 648
  • 5
  • 15