0

With the Node JS request module it is possible to get the response, but, is there any way of getting the request headers sent?

1 Answer 1

2

I'm not sure what the official way of doing this is but there are several things that seem to work.

If you haven't bound the callback to another this value then it will just be the request, e.g.:

request.get(options, function() {
    console.log(this.getHeader('... header name ...'));
    console.log(this.headers);
});

You could also access the request using response.request:

request.get(options, function(err, response) {
    console.log(response.request.getHeader('... header name ...'));
    console.log(response.request.headers);
});

That second approach should work anywhere that you have access to the response.

I believe these are the relevant lines in the source code:

https://github.com/request/request/blob/253c5e507ddb95dd88622087b6387655bd0ff935/request.js#L940

https://github.com/request/request/blob/253c5e507ddb95dd88622087b6387655bd0ff935/request.js#L1314

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.