Get Content Type of Request

mahemoff picture mahemoff · Mar 5, 2013 · Viewed 33.4k times · Source

To find the incoming content type, docs say:

 request.headers["Content-Type"] # => "text/plain"

But I found by trial-and-error, that doesn't work, but this does:

 request.headers["CONTENT_TYPE"]=='application/json'

So what's the most robust+portable way to do it?

Answer

DRobinson picture DRobinson · Mar 5, 2013

I would usually go for request.format and request.content_type for reading these header fields.

EDIT: found a bit more on this that might help: https://stackoverflow.com/a/1595453/624590