How to get error status code of http get NodeJS

+1 vote

 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.).  I got 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!

Jun 2, 2020 in Java-Script by kartik
• 37,520 points
4,146 views

1 answer to this question.

0 votes

Hello @kartik,

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

Try response.statusCode in this:

request.on('response', function (response) {}); 

Hope this help!!

Thank You!!

answered Jun 2, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
3 answers
0 votes
1 answer

How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

Hello @kartik, The error callback will be executed ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
17,248 views
0 votes
1 answer

How to get “value” of file upload input field in jquery?

Hello @kartik, Yes ,you can read the value ...READ MORE

answered Jul 8, 2020 in Java-Script by Niroj
• 82,840 points
12,907 views
0 votes
1 answer

How to get the jQuery $.ajax error response text?

Hello @kartik, Try: error: function(xhr, status, error) { ...READ MORE

answered Jul 27, 2020 in Java-Script by Niroj
• 82,840 points
31,519 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,994 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,685 views
0 votes
1 answer

How to get the directory of the currently running file?

Hello @kartik, This should do it: import ( ...READ MORE

answered May 6, 2020 in Java-Script by Niroj
• 82,840 points
1,384 views
0 votes
1 answer

How to get rid of underline for Link component of React Router?

Hello, If you are using styled-components, you could do ...READ MORE

answered May 18, 2020 in Java-Script by Niroj
• 82,840 points
12,382 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP