Hapi: What is the best way to access request headers?

Created on 22 Oct 2013  Â·  5Comments  Â·  Source: hapijs/hapi

In reading through the docs it wasn't clear what is the best way to access the request headers (or what is the hapi way).

I realize that one can go to request.raw.req.headers but from the docs, we are told to avoid using raw where possible, so is there a better way?

I understand that it is nice to abstract things away where possible, and for some things like auth, one could read the headers in a plugin and then set something in the artifacts.

Maybe that's the recommended approach for other things too, read them in a plugin and set some business state in a plugin?

Thanks in advance for any suggestions or advice. I am enjoying using Hapi.

feature

Most helpful comment

Just use request.raw.req.headers. Raw is fully supported. I'll add request.headers but that will just be a reference to the above.

All 5 comments

Some examples of headers that are commonly needed are:

  • range
  • content-md5
  • accept-language
  • accept
  • if-match, if-none-match, if-* for put, post, delete
  • referer
  • upgrade
  • custom headers added by an authorizing proxy for user and role (this one might be done as good auth plugin but still could be good to have direct read access)

So it would be good to have a Hapi strategy for accessing these and any others needed.

Thoughts?

I'm not familiar with another method (don't believe it exists), but using
raw.req.headers for reads (as opposed to CUD) doesn't have side-effects
so is perfectly save.
In fact the reference makes use of them:
https://github.com/spumko/hapi/blob/master/docs/Reference.md#stream

On Thu, Oct 24, 2013 at 5:27 PM, Jeff Barczewski
[email protected]:

Some examples of headers that are commonly needed are:

  • range
  • content-md5
  • accept-language
  • accept
  • if-match, if-none-match, if-* for put, post, delete
  • referer
  • upgrade
  • custom headers added by an authorizing proxy for user and role (this
    one might be done as good auth plugin but still could be good to have
    direct read access)

So it would be good to have a Hapi strategy for accessing these and any
others needed.

Thoughts?

—
Reply to this email directly or view it on GitHubhttps://github.com/spumko/hapi/issues/1114#issuecomment-27002119
.

Just use request.raw.req.headers. Raw is fully supported. I'll add request.headers but that will just be a reference to the above.

Thanks Eran. Adding request.headers will be great so we don't have to access core object which could change in future. Good to have a light abstraction for some key things.

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kevinsimper picture kevinsimper  Â·  4Comments

hueniverse picture hueniverse  Â·  4Comments

shamsher31 picture shamsher31  Â·  5Comments

hbinduni picture hbinduni  Â·  5Comments

RohovDmytro picture RohovDmytro  Â·  4Comments