Request headers name normalization

52 views
Skip to first unread message

Roman

unread,
Oct 18, 2014, 4:49:04 PM10/18/14
to rubyonra...@googlegroups.com
I believe, there's inconsistency between how Rails and curl normalize header names.

If I do a `curl -H "some_header: value"` it will send the header as `HTTP_SOME_HEADER`. Then in Rails controller I would expect that header to be accessible as `request.headers[:some_header]`, but it's not. The culprit is in the `ActionDispatch::Http::Headers#env_name`, which doesn't adjust the header name if it contains an underscore.

Which of the two is correct? Do you think the issue could addressed in Rails?

and...@benjamin.dk

unread,
Oct 23, 2014, 3:59:23 AM10/23/14
to rubyonra...@googlegroups.com
I am not sure I understand your question, but how does the headers hash look like? maybe if you show me the result will be easier to understand the mistake!

Also gives a concrete example of which headers you trying to set.

I have always done curl requests like this:

curl -i --header "Accept: application/json" --header "Content-Type: application/json"

the -i option from the manual:

 -i, --include
              (HTTP) Include the HTTP-header in the output. The HTTP-header includes  things  like  server-
              name, date of the document, HTTP-version and more...

so you can see the answer to your curl call.

Roman

unread,
Oct 23, 2014, 4:39:40 AM10/23/14
to rubyonra...@googlegroups.com
I figured, it's not related to curl but to Rails and Rack only.
Here's what I do:

  curl -v -H "hello_world: true" localhost:3000/empty

Now in the controller I dump the request headers and see "HTTP_HELLO_WORLD"=>"true". Rack converted "hello_world" to "HTTP_HELLO_WORLD".
But if I try to access the header like `request.headers[:hello_world]` or `request.headers["hello_world"]`, it will return nil.

If I used 'hello-world' instead of 'hello_world' (underscore instead of dash), everything would have worked as expected.

Anuj Dutta

unread,
Oct 23, 2014, 4:56:35 AM10/23/14
to rubyonra...@googlegroups.com
Hello Roman,

I wrote a blog post about it a while ago:


I hope it helps.

Anuj



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/22f6a897-2cda-45f5-bbad-32b55e74a5d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Anuj DUTTA

Roman

unread,
Oct 23, 2014, 5:13:14 AM10/23/14
to rubyonra...@googlegroups.com
Thanks, Anuj. I didn't know that nginx also makes its contribution to confusion with headers.
Reply all
Reply to author
Forward
0 new messages