Search code examples
ruby-on-railsruby-on-rails-4actiondispatch

Remove the HTTP_ prefix in Rails headers


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?


Solution

  • 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.