3

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?
shingara
  • 46,608
  • 11
  • 99
  • 105
John
  • 9,254
  • 12
  • 54
  • 75

1 Answers1

5

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.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • Thanks! Great explanation. So am I correct in saying that `request.content_type` is more of a safety/convenience mechanism for those lazy people who fail to set the `Content-Type` on the request header? In other words, if I am **expecting** a **certain** `Content-Type`, I should be checking `request.env['CONTENT_TYPE']` and **not** `request.content_type` as Rails will do guess-work to figure out this value based on the actual content. – John Feb 01 '11 at 15:30