Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force all received requests Content-Type to JSON

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. :(

like image 533
Mich Avatar asked Apr 10 '14 13:04

Mich


People also ask

How do you set the Content-Type of the response to JSON?

The correct MIME media type for JSON is application/json . JSP will use it for sending a response to the client. Show activity on this post. “ application/json ” is the correct JSON content type.

What is header (' Content-Type application JSON?

Content-Type: application/json is just the content header. The content header is just information about the type of returned data, ex::JSON,image(png,jpg,etc..),html. Keep in mind, that JSON in JavaScript is an array or object.

Is it necessary to set the response's header as Content-Type application JSON?

This means when you're sending JSON to the server or receiving JSON from the server, you should always declare the Content-Type of the header as application/json as this is the standard that the client and server understand.

How get JSON data from GET request in Python?

To request JSON from a URL using Python, you need to send an HTTP GET request to the server and provide the Accept: application/json request header with your request. The Accept header tells the server that our Python client is expecting JSON.


2 Answers

You could define a any type response inside the respond_to block, which will not restrict your controller to respond when request uri ends with .json, it also relief you from defining a response type explicitly and will keep, independently of request content-type, responding as you wish, example:

respond_to do |format|
  format.any  {render :json => {foo: 'bar'}}
end
like image 93
ErvalhouS Avatar answered Oct 04 '22 10:10

ErvalhouS


Check this SO answer. It uses constraints as follows

defaults format: :json do
  # your v1/registration route here
end
like image 31
Tun Avatar answered Oct 04 '22 09:10

Tun