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,510 points
3,867 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,880 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,880 points
16,307 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,880 points
12,055 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,880 points
29,257 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,880 points
21,876 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,880 points
2,680 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,880 points
2,542 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,880 points
41,724 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,880 points
1,179 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,880 points
11,153 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