Content-Type "application / json" not required on rails

I have an API endpoint in rails, which by default, if you do not set any Content-Type headers, process the parameters in

application/x-www-form-urlencoded 

Is there a way to handle the json string in rails from a POST request without specifying the type of content in the header?

+6
source share
2 answers

In your routes.rb you can put the POST route in the namespace and define a format that can be expected as follows:

 namespace :api, defaults: { format: :json } do post 'example' => 'controller#action' end 

In this case, you load your json payload into '/api/example.json' and should not define a Content-Type. You can also define it for only one line of the message, but I have not tested this.

0
source

Correct answer

 namespace :api, defaults: { format: :json } do resources :users end 
0
source

Source: https://habr.com/ru/post/972806/


All Articles