I'm actually working on an API which uses Rails 4. I would like to set the Content-Type
of a request to JSON if the client does not specify a media type in Content-Type
header.
In order to get that behaviour I tried to add the following before_action
in my ApplicationController
:
def set_request_default_content_type
request.format = :json
end
In my RegistrationsController#create
method I have a breakpoint to check if everything is working. Well, the request.format
trick does not work, despite the value is set to application/json
it seems that the controller (or Rails internals) do not consider the received request's Content-Type as JSON.
I did a POST request with the following body (and no Content-Type) :
{"user" : {"email":"[email protected]","password":"foobarfoo"}}
By debugging with Pry I see that :
[2] api(#<V1::RegistrationsController>) _ request.format.to_s
=> "application/json"
[3] api(#<V1::RegistrationsController>) _ params
=> {
"action" => "create",
"controller" => "v1/registrations"
}
It means that Rails did not have considered my request with the request.format configured with Mime::JSON
, but instead with Mime::ALL
and so it didn't parse the request's JSON body. :(
ActionController
– BergmansActionController::ParamsWrapper
is enabled by default for the json format. But it doesn't work. At the moment I don't use MetalController but a classic Rails architecture with standard controllers. – Prostrate