views:

805

answers:

3

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

A: 

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.

Luke Francl
you're right.. what I didn't mention is that while curl is capable of sending PUT requests, most browsers aren't and I'm writing in flex, which is not capable of it. Therefore I have to choose between GET and POST and try to make it understand it's a PUT...
luca
Makes sense. Did you try lower case 'put' as your _method parameter? I looked at a form generated by Rails (update forms use PUT) and it used this code: `<input name="_method" type="hidden" value="put" />`.
Luke Francl
What does /people/12 have to do with REST?
Wahnfrieden
Wahnfrieden, that's a standard format for Rails' RESTful routing. See http://guides.rubyonrails.org/routing.html
Eric Nguyen
+1  A: 

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.

luca
+1  A: 

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\":\"[email protected]\"}}"
  www.site.com/users/username.json
Joost Baaij