Remove the HTTP_ prefix in Rails headers

2.3k views Asked by At

Any request sent to a Rails controller gets an HTTP_ prefixed to it, as that's appended by ActionDispatch::HTTP.Headers. Is there a way to prevent that (without overriding ActionDispatch::HTTP, so that I can use my custom headers, as is and use those as keys for headers.@env?

1

There are 1 answers

2
Simone Carletti On BEST ANSWER

No, it's not possible. That's how the ActionDispatch::Http::Headers class is designed to normalize the headers.

private

def env_name(key)
  key = key.to_s
  if key =~ HTTP_HEADER
    key = key.upcase.tr('-', '_')
    key = "HTTP_" + key unless CGI_VARIABLES.include?(key)
  end
  key
end

You can still use your custom headers. You just need to reference them as HTTP_X_FOO instead of x-foo.