views:

1014

answers:

3

here's my problem.. resource: user method: create

I call curl like this:

curl -X POST -H 'Content-type: text/xml' -d '<xml><login>john</login><password>123456</password></xml>' http://0.0.0.0:3000/users

but the params hash in rails look like this:

{"xml"=> {"login"=>"luca", "password"=>"123456"}}

I want it to look like this:

{"login"=>"luca", "password"=>"123456"}

I can get it to be like that if I pass the parameters on the url (...?login=luca&pas....), but that's not good...

any idea?

+2  A: 
curl -X POST -H 'Content-type: text/xml' -d '<login>john</login><password>123456</password>' http://0.0.0.0:3000/users

What does that get you?

After getting your comment, why not access it through params[:xml] in your controller, rather than params[:login] and params[:password]?

@user = User.authenticate(params[:xml])

That will pass login and password to your model.

Mike Trpcic
you can't add 2 nodes.. you'll get:"attempted adding second root element to document"
luca
yes, that is how I'm now circumventing the problem, but I was hoping to better understand why it doesn't work..
luca
It doesn't work because '<login>john</login><password>123456</password>' is not a well formed XML document. The root node ends at </login>.For root nodes and Immortals, "there can be only one." :)
Jamie Flournoy
+1  A: 
curl -X POST -d 'login=john&password=123456' http://0.0.0.0:3000/users

See also: Stack Overflow: How do I make a POST request with curl

Jamie Flournoy
luca
+1  A: 

Hey I had this problem for a long time as well. I found out that you can do this -

Make a new defaults.rb inside the initializers directory: yourapp/config/initializers/defaults.rb

ActiveRecord::Base.include_root_in_json = false

Now the root should be gone from your json.

Synthesis