4

How to use curl to make a http post on a form with nested attributes using application/x-www-form-urlencoded, instead of application/xml?

With XML it works just fine:

curl -i -X 'POST' -H 'Content-Type: application/xml' http://localhost:3000 -d '<user><name>John Doe</name><emails><email><address>[email protected]</address></email><email><address>[email protected]</address></email></emails></user>'

And the result:

Parameters: {"action"=>"profile", "controller"=>"users", "user"=>{"name"=>"John Doe", "emails"=>{"email"=>[{"address"=>"[email protected]"}, {"address"=>"[email protected]"}]}}}

But, I'm trying to accomplish the same result without xml.

I tried like this:

curl -i -X POST -d 'user[name]=John Doe&user[emails][email][address][email protected]&user[emails][email][address][email protected]' http://localhost:3000/

But it didn't worked:

Parameters: {"user"=>{"name"=>"John Doe", "emails"=>{"email"=>{"address"=>"[email protected]"}}}}

1 Answer 1

9

would you please try the following:

curl -i -X POST -d 'user[name]=John Doe&user[emails][][email][address][email protected]&user[emails][][email][address][email protected]' http://localhost:3000/

Note the [] behind [emails]

Sign up to request clarification or add additional context in comments.

3 Comments

Almost: Parameters: {"user"=>{"name"=>"John Doe", "emails"=>[{"email"=>{"address"=>"[email protected]"}}]}} The array part works, but only the last email is being considered.
Hmm, this works. : curl -i -X POST -d 'user[name]=John Doe&user[emails][][email protected]&user[emails][][email protected]' http://localhost:3000/
Thanks, Michael. In your last comment you've forgotten the address attribute. But with your help I was able to figure out: curl -i -X POST -d 'user[name]=John Doe&user[emails][email][][address][email protected]&user[emails][email][][address][email protected]' http://localhost:3000/. The [] is after [email]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.