Reading custom request headers

I’m trying to find some docs on how to read custom request headers in Phoenix? There are a few optional request headers that a client may include in a request that we want to listen for. Sorry to ask such a basic RTFM question, but I haven’t been able to find any docs on this. Thanks!

It’s in the Plug doc rather than Phoenix itself

https://hexdocs.pm/plug/Plug.Conn.html#get_req_header/2

1 Like

Just in case anyone is in the same boat, the Plug.RequestId plug already handles this! By default, it will listen for an incoming request header named “x-request-id” (FYI: all header names are normalized for lowercase), and so long as the ID is between 20 and 200 characters, it will be used as the request header. The Plug.RequestId is referenced in a Phoenix 1.3 app inside your MyAppWeb.Endpoint module, and that’s where you can set a custom header if desired:

plug Plug.RequestId, http_header: "custom-request-id"
1 Like

I am a bit confused.

Are we reading or setting the custom header here?

plug Plug.RequestId, http_header: "custom-request-id"

As far as I remember, this plug reads the incoming custom header and then uses that value to identify the request instead of generating a new unique identifier (the request ID could be used for log messages, for example). By allowing an incoming request to specify its ID, that makes it easier to associate whatever Phoenix logs etc. with whatever sent the request. Hope that makes sense.