I have weird case. I have a RoR app, which provides REST API which I'm connecting to from Java application.
I'm developing RoR locally, and deploying it on Heroku environment.
Regardless how (I tried from Java APP, Mozilla REST client, etc.) I try to send POST HTTP request that should be handled by create action in api controller. On localhost - everything is working as expected. On Heroku production env - the POST request is treated as normal GET.
Here are my routes for this resource:
api_v1_items GET /api/v1/items(.:format) api/v1/items#index {:format=>:json}
POST /api/v1/items(.:format) api/v1/items#create {:format=>:json}
api_v1_item GET /api/v1/items/:id(.:format) api/v1/items#show {:format=>:json}
PATCH /api/v1/items/:id(.:format) api/v1/items#update {:format=>:json}
PUT /api/v1/items/:id(.:format) api/v1/items#update {:format=>:json}
DELETE /api/v1/items/:id(.:format) api/v1/items#destroy {:format=>:json}
So I'm trying to do POST request to /api/v1/items
passing all necessary parameters.
In localhost the response is correct:
Started POST "/api/v1/items?token=l4XOHrhDApPqTp1u4TxBjQ" for 127.0.0.1 at 2014-05-15 22:11:49 +0200
Processing by Api::V1::ItemsController#create as JSON
Parameters: {"height"=>10.0, "item_name"=>"Super item", "width"=>20.0, etc...
However the same request fired at Heroku its treated as GET:
2014-05-15T20:27:58.137541+00:00 app[web.1]: Started GET "/api/v1/items?token=iEdDkDLiDUlWi0mDbr6XYw" for 89.74.57.51 at 2014-05-15 20:27:58 +0000
2014-05-15T20:27:58.223620+00:00 app[web.1]: Processing by Api::V1::ItemsController#index as JSON
Any idea? Of course both repos are in sync. Checked few times.
This is really weird... maybe some kind of Heroku cache magic?
I experienced a similar error when not using a custom domain just because of an easily overlooked error: I was using heroku.com instead of herokuapp.com
wrong: http://my-app.heroku.com
right: http://my-app.herokuapp.com
I suspect it's very similar in cause to the issue mentioned in Catsby's answer.
301 redirects are not Heroku magic. Your DNS (or possibly your app) is likely forwarding all apex requests (mydomain.com) to the
www
subdomain.Using subdomains is preferred:
I had this same issue when sending a POST request to heroku using HTTP instead of HTTPS. Every time heroku routed my POST requests as GET requests. Once I updated the url to use HTTPS, my POST requests were routed by heroku as POSTs and not GETs, resolving the issue. The redirection issues mentioned in the previous posts are likely the root cause of the issue I experienced as well.
Well, I tried CURL, and it appeared error is silly.
I was posting at http://mydomain.com, where it's routed as GET. When I fire at http://www.mydomain.com - it works.
Heroku magic.
Below are curl's and results for your reference. Maybe somebody will be able to explain why it works like this...
POST at mydomain.com
POST at www.mydomain.com