What is the and why is there a difference between request.env['CONTENT_TYPE'] and request.content_type?

1000 views Asked by At

I'm seeing two different values for request.env['CONTENT_TYPE'] and request.content_type.

From a separate application, I am sending a request to my Rails application and have explicitly set the Content-Type to text/xml.

Here is what I am seeing on the other end, from within my Rails application:

request.env['CONTENT_TYPE'] = "text/xml"
request.content_type = "application/xml"

request.content_type is actually action_dispatch.request.content_type

  1. What is the difference between request.env['CONTENT_TYPE'] and request.content_type?
  2. Why are these two values different?
1

There are 1 answers

1
Ryan Bigg On BEST ANSWER

request.env contains Rack's "thoughts" on what the content type is. Generally, this is the content type of the request that you have made.

request.content_type on the other hand is Rails's interpretation of what it thinks the content type is, based off the format of the request. These are defined in a file called mime_types.rb in Rails (I can't recall which part, but with that you should be able to locate it), and additional ones can be specified in config/initializers/mime_types.rb.