Question

if I do this:

curl -X POST -H 'Content-type: application/xml' -d '<person>...</person>' 'http://0.0.0.0:3000/people/12?_method=PUT'

I get "Only get, put, and delete requests are allowed." Why? Rails 2.3.2

Was it helpful?

Solution 2

This is actually a malfunctioning in rails recent versions. The solution is to pass the HTTP method override header with put. Important:the full name of the header is http-x-http-method-override but rails adds the http prefix so you should trim that part.

OTHER TIPS

luca is correct, you need to send the X-Http-Method-Override header with your request. Putting _method=put in the JSON body did not work for me. Your curl command line will look like this:

 curl
  -H "X-Http-Method-Override: put"
  -H "Content-Type: application/json"
  -d "{\"user\":{\"email\":\"blah@example.com\"}}"
  www.site.com/users/username.json

There's a couple of things that could be going wrong.

First off, you're sending a POST with curl, but you've got the "magic" _method parameter on your URL, and it's set to PUT. Based on the error, it sounds like Rails is taking POST as the authoritative request type. Try it without that and see what happens.

On a more pedestrian error, the routing (in config/routes.rb could be configured to not allow POST requests for for the action you are attempting to use.

If you're using a standard RESTful controller, that makes good sense, because you're accessing a member (/people/12) which by default only allows GET, PUT, and DELETE.

I would try using curl -X PUT and dropping the ?_method=PUT from your request.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top