After having heard about nodejs for so long now , i decided to play with it today and unfortunately ended up in frustation when i was not able to make the below <20 lines code execute successfully.
var connection = http.createClient(80, "localhost");
var request = connection.request('GET', "/");
request.end();
request.on('response', function(response){
var result = "";
console.log(response.statusCode)
response.on('data', function(chunk){
result += chunk;
})
response.on('end', function(){
console.log(result);
})
});
connection.addListener('error' , function (connectionException){
console.log("ERROR occured while calling Rotten Tomatoes Api "+connectionException);
})
I was initially making call to rottentomatoes api and was getting 400 , Bad request.I changed hostname to localhost ( i had apache running locally) and was more frustated to see the same 400. This prompted me to check the apache error_log and this is what i was getting there
client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23):
and little googling told me that nodejs does not send the host header by default [Stumped]. so i had to manually add the host header like below.
var request = connection.request('GET', "/" , {"Host":"localhost");
I am not sure but i think it should be a good idea to include some common headers by default in the request sent by nodejs.
No comments:
Post a Comment