21

If I need to see the incoming request headers I use: req.headers.

I need to see the list of all headers that will be in the response.

res.headers is undefined.

I know I can set response headers: res.header('', '').

How do I view the response headers..?

Stephen Last
  • 5,491
  • 9
  • 44
  • 85
  • 1
    did you try just calling the function with no parameters and seeing what it returns? – nem035 Jun 29 '16 at 15:16
  • 1
    @nem035 Thanks.. `res.header()` returns an object with a `_headers` property. Whenever I've tried working with the `req`/`res` objects before they just fill my console window and I can't see the whole object. Thought someone might be able to point me quickly in the right direction. – Stephen Last Jun 29 '16 at 15:23

4 Answers4

26

In recent versions of express there is:

res.getHeaders()

 -> {x-powered-by: "Express"}

Properties starting with "_" are not part of the official api. In case of changes, they will not be documented, and chances are the code will break.

Alex
  • 320
  • 4
  • 9
11

Thanks to @nem035.

Response headers: res.header()._headers

Community
  • 1
  • 1
Stephen Last
  • 5,491
  • 9
  • 44
  • 85
10

Also:

res.get('Content-Type');
// => "text/plain"

https://expressjs.com/en/api.html#res.get

and, confusingly, lowercase is fine:

res.get('content-type');
// => "text/plain"

res.getHeaders()['content-type']
// => "text/plain"
ptim
  • 14,902
  • 10
  • 83
  • 103
  • 1
    Confusing to the developer, but not to the implementer. Headers are case insensitive, which is why implementers and vendors are falling back to lower case. https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive – eljefedelrodeodeljefe May 22 '20 at 18:57
-2

You Can Use This Method ...

/* ----------------------- Get Response Header By Name ---------------------- */
/* res.getHeader('Put Header Name Here'); */
/* For Example */
res.getHeader('x-powered-by');

This Method Help You To Get Response Header Name