Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http get NodeJS how to get error status code?

OK, I must be dense since I cannot find anywhere how to get the error status codes when using Node.JS http.get or http.request.

My code:

var deferred = $q.defer(); var req = https.get(options, function(response) {   var str = '';   response.on('data', function(chunk) {     str += chunk;   });   response.on('end', function() {     console.log("[evfService] Got user info: " + str);     deferred.resolve(str);   }); });  req.on('error', function(e) {   deferred.reject(e); }); 

In that "req.on" bit, what I want is the http status code (i.e. 401, 403, etc.). What I get is a semi-useless error object that does not give me the code or any reference to the response object.

I have tried intercepting in the function(response) callback, but when there is a 404, it never gets called.

Thanks!

like image 690
user3647766 Avatar asked May 17 '14 14:05

user3647766


People also ask

How do I check Nodejs error code?

on('error', function(e) { console. error(e); }); If you try that with (say) an unknown resource, you'll see statusCode: 404 .

How can I get the status code from an HTTP error in fetch?

To get the status code of an HTTP request made with the fetch method, access the status property on the response object. The response. status property contains the HTTP status code of the response, e.g. 200 for a successful response or 500 for a server error.

What is statusCode in Nodejs?

Status codes are a set of universal web development codes utilized to create apps all over the world. Informational responses ( 100–199 ) Successful responses ( 200–299 ) Redirects ( 300–399 ) Client errors ( 400–499 )


2 Answers

Your callback gets called regardless of the response status code from the server, so within your callback, check response.statusCode. That is, a 4xx status code isn't an error at the level you're working at; the server responded, it's just that the server responded by saying the resource wasn't available (etc.)

This is in the documentation but characteristically vague. Here's the example they give, with a comment pointing to the relevant bit:

var https = require('https');  https.get('https://encrypted.google.com/', function(res) {   console.log("statusCode: ", res.statusCode); // <======= Here's the status code   console.log("headers: ", res.headers);    res.on('data', function(d) {     process.stdout.write(d);   });  }).on('error', function(e) {   console.error(e); }); 

If you try that with (say) an unknown resource, you'll see statusCode: 404.

So for what you're doing, you may want something like this:

var deferred = $q.defer();  var req = https.get(options, function (response) {     var str = "";      if (response.statusCode < 200 || response.statusCode > 299) { // (I don"t know if the 3xx responses come here, if so you"ll want to handle them appropriately         response.on("data", function() { } ); // ¹         deferred.reject(/*...with appropriate information, including statusCode if you like...*/);     }     else {         response.on("data", function (chunk) {             str += chunk;         });         response.on("end", function () {             console.log("[evfService] Got user info: " + str);             deferred.resolve(str);         });     } });  req.on("error", function (e) {     deferred.reject(/*...with appropriate information, but status code is irrelevant [there isn"t one]...*/); }); 

¹ The empty data event handler in the branch handling non-OK status codes is there because of this note in the documentation:

...if a 'response' event handler is added, then the data from the response object must be consumed, either by calling response.read() whenever there is a 'readable' event, or by adding a 'data' handler, or by calling the .resume() method. Until the data is consumed, the 'end' event will not fire. Also, until the data is read it will consume memory that can eventually lead to a 'process out of memory' error.

Since we're passing a function to https.get, we're hooking the 'response' event, which suggests we need to do one of those things (in this case, I've added a do-nothing data handler). Thanks to Nicolas2bert for pointing that out!.

like image 175
T.J. Crowder Avatar answered Oct 07 '22 15:10

T.J. Crowder


An error code 400 response is not considered an error by node.js.

Try response.statusCode in this:

request.on('response', function (response) {});  
like image 45
thebiggestlebowski Avatar answered Oct 07 '22 15:10

thebiggestlebowski