1

Below in this example, in the variable 'obj' i get body of response. How to get header values of response using this https node.js library?

var options = {
    hostname: hostname,
    port: port,
    path: pathMethod,
    method: method,
    headers: {
        'Content-Type': APPLICATION_JSON,
        'Authorization': BEARER + localStorage.jwtToken
    },
    rejectUnauthorized: false,
    agent: false,
    requestCert: false

};

return new Promise(function(resolve, reject) {
var req = https.request(options, function(res) {
        res.setEncoding(ENCODING_UTF8);
        res.on('data', function(result) {

            try {
                const obj = JSON.parse(result);
                resolve({ 'httpStatus': PAGE_STATUS_200, 'result': obj });
            }
            catch(error) {
                console.error(error);
                resolve(resolve({ 'httpStatus': PAGE_STATUS_500 }));
            }

        });

        res.on('end', () => {
            console.log('No more data in response.');
        });
    });

    req.on('error', function(err) {
        console.log(`problem with request: ${err.message}`);
        reject(err);
    });

    if (postData) {
        req.write(postData);
    }

    req.end();
});

In my browser i get all necessary headers. What could be the problem that i can not get headers with https node.js lib?

alexis_dia
  • 156
  • 3
  • 13
  • `res.headers` will contain the response headers. – Marcos Casagrande May 21 '18 at 13:52
  • Possible duplicate of [Getting HTTP headers with node.js](https://stackoverflow.com/questions/5922842/getting-http-headers-with-node-js) – Marcos Casagrande May 21 '18 at 13:52
  • I've tried with your solution, but get only 4 parameters in comparison with Chrome browser where I see 15 params(Including JWT Token). Also tried request using Postman and got the same 11 parameters in the response header. But I need to get one appropriate parameter(JWT Token) that I can not get using https node.js lib. – alexis_dia May 21 '18 at 14:23
  • Hello everyone. Here is the answer to this question. https://github.com/infinitered/apisauce/issues/110 and https://stackoverflow.com/questions/37897523/axios-get-access-to-response-header-fields In short, the problem was in the backend of Spring Boot 2. – alexis_dia Jun 05 '18 at 09:39

2 Answers2

1

The response headers should be available in the res.headers object, e.g.

// Log headers
console.log('Headers: ', res.headers);

See: https://nodejs.org/api/https.html

e.g.

const https = require ('https');


// This will return the IP address of the client
var request = https.request({ hostname: "httpbin.org", path: "/ip" },  (res) => {
    console.log('Headers: ', res.headers);
    res.on('data', (d) => {
        console.log('/ip response: ', d.toString());    
    });
});

// Also try using Request library

var request = require('request');

var options = {
    url: "https://httpbin.org/ip",
    method: "get"
};

console.log('Requesting IP..');
request(options, function (error, response, body) {
    if (error) {
        console.error('error:', error);
    } else {
        console.log('Response: Headers:', response && response.headers);
    }
});
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • Same kind of explanation. This is good too. +1 to your answer. :) – Harshal Yeole May 21 '18 at 13:59
  • The same explanation provided on the original question, this is clearly a duplicate. – Marcos Casagrande May 21 '18 at 13:59
  • Did, as you advised, but in the answer from code came only 4 parameters, against 15 in the browser. In my case, I need all the parameters. – alexis_dia May 21 '18 at 14:02
  • Have you tried using curl for this purpose? e.g. curl -v url (curl http://httpbin.org/ip -v). Surprising that we get less headers when calling the same url from two different sources.Could you maybe show us the headers you get in the browser vs node.js? – Terry Lennox May 21 '18 at 14:04
  • I sent the request using Postman and got the same 11 parameters in the response header. But I need to get at least that 11 parameters from the code using https node.js lib. – alexis_dia May 21 '18 at 14:15
  • I'd almost suggest logging a bug for this, looks like you have a reproducible case. – Terry Lennox May 21 '18 at 14:34
  • Have you tried using the Request library for this in node.js? Do you get the same answer. – Terry Lennox May 21 '18 at 14:35
1

You can get the headers in https module.

This is how you get the headers for the response.

res.headers

I have updated your code in example below:

    var req = https.request(options, function(res) {
    res.setEncoding(ENCODING_UTF8);
    res.on('data', function(result) {

    console.log("Headers: ", res.headers);

    // Your code here.

    });

    res.on('end', () => {
    // Do something here.
    });
});

Hope this helps.

Harshal Yeole
  • 4,812
  • 1
  • 21
  • 43
  • Thanks for the answer. I've tried with your solution, but get only 4 parameters in comparison with Chrome browser where I see 15 params(Including JWT Token). Also tried request using Postman and got the same 11 parameters in the response header. But I need to get one appropriate parameter(JWT Token) that I can not get using https node.js lib. – alexis_dia May 21 '18 at 14:24
  • What exactly you want from headers? – Harshal Yeole May 21 '18 at 14:40
  • I want to get one needful parameter(JWT Token) from response header using https node.js lib. But i get 4 unnecessary parameters in response header using https node.js lib. When i am using Postman, i get all parameters, including needful parameter(JWT Token). How i can get all parameters or only one needful parameter(JWT Token) using https node.js lib? – alexis_dia May 21 '18 at 14:45
  • How is the JWT token in res header? It should be in request header – Harshal Yeole May 21 '18 at 14:57
  • Yes, JWT token also is in request header at front-side. Also in the backend-side JWT token is put in response header. – alexis_dia May 21 '18 at 15:04
  • Best possible way, according to me to achieve goals would be changing the backend functionality to send thee token body then. – Harshal Yeole May 21 '18 at 15:08
  • Sometimes backend need to refresh the token automatically at random time and send it to the front. Front-end should keep and store token on each rest call. There is not better place than response headers. Adding relevant token to each backend object which produce json is not perfect practice. – alexis_dia May 22 '18 at 11:50