Howdy,
I am making a few AJAX endpoints using rails, and noticed the following
inconsistent behavior when setting the Content-Type to application/json.
Here are four different requests to show the problem I am having.
- First, when I make the following example request:
curl -X GET
“http://localhost:3000/api/v1/events?counts=true&order[column]=occurs_at&order[direction]=asc&page[number]=1&page[limit]=100”
Rails shows the following in the log:
Started GET
“/api/v1/events?counts=true&order%5Bcolumn%5D=occurs_at&order%5Bdirection%5D=asc&page%5Bnumber%5D=1&page%5Blimit%5D=100”
for 127.0.0.1 at 2013-04-18 03:57:49 +0000
Processing by Api::V1::EventsController#index as JSON
Parameters: {“counts”=>“true”, “order”=>{“column”=>“occurs_at”,
“direction”=>“asc”}, “page”=>{“number”=>“1”, “limit”=>“100”}}
- However, if I change the Content-Type to be “application/json”:
curl -X GET -H “Content-Type: application/json”
“http://localhost:3000/api/v1/events?counts=true&order[column]=occurs_at&order[direction]=asc&page[number]=1&page[limit]=100”
I get the following:
Started GET
“/api/v1/events?counts=true&order%5Bcolumn%5D=occurs_at&order%5Bdirection%5D=asc&page%5Bnumber%5D=1&page%5Blimit%5D=100”
for 127.0.0.1 at 2013-04-18 03:57:33 +0000
Processing by Api::V1::EventsController#index as JSON
Parameters: {“counts”=>“true”, “order”=>{“column”=>“occurs_at”,
“direction”=>“asc”}, “page”=>{“number”=>“1”, “limit”=>“100”},
“event”=>{}}
The bold part is the extra parameter (in this case “event”) that gets
added
by making this a JSON request.
- The problem, is that this event parameter will clobber any parameter
I
send over also called event. For example:
curl -X GET -H “Content-Type: application/json”
“http://localhost:3000/api/v1/events?counts=true&order[column]=occurs_at&order[direction]=asc&page[number]=1&page[limit]=100&
event=Super%20Important%20Info”
And it is not parsed from the params:
Started GET
“/api/v1/events?counts=true&order%5Bcolumn%5D=occurs_at&order%5Bdirection%5D=asc&page%5Bnumber%5D=1&page%5Blimit%5D=100&
event=Super%20Important%20Info” for 127.0.0.1 at 2013-04-18 04:02:56
+0000
Processing by Api::V1::EventsController#index as JSON
Parameters: {“counts”=>“true”, “order”=>{“column”=>“occurs_at”,
“direction”=>“asc”}, “page”=>{“number”=>“1”, “limit”=>“100”},
“event”=>{}}
- But, if I dont pass the JSON content type:
curl -X GET
“http://localhost:3000/api/v1/events?counts=true&order[column]=occurs_at&order[direction]=asc&page[number]=1&page[limit]=100&
event=Super%20Important%20Info”
It works just fine:
Started GET
“/api/v1/events?counts=true&order%5Bcolumn%5D=occurs_at&order%5Bdirection%5D=asc&page%5Bnumber%5D=1&page%5Blimit%5D=100&
event=Super%20Important%20Info” for 127.0.0.1 at 2013-04-18 04:02:22
+0000
Processing by Api::V1::EventsController#index as JSON
Parameters: {“counts”=>“true”, “order”=>{“column”=>“occurs_at”,
“direction”=>“asc”}, “page”=>{“number”=>“1”, “limit”=>“100”},
“event”=>“Super
Important Info”}
Is there any way I can just turn off this functionality? I think it
might
be because this is an EventsController, and Rails is trying to give me
something for free, but I just want to send over whatever I want,
especially as I will have situations like an OrdersController, that I
want
to pass an order to (such as id asc, etc…).
Thanks for any help,
Robert