Using cURL to test RESTful Rails Web Services

Lately, I've been writing lots of services for some of our newer Rails 2.0 projects. And one of the quickest/easiest ways I've found to debug these web service apps from a Mac is by using cURL. I'm not exactly an expert on the subject, so I'm documenting this half for myself and half to help anyone else out....

First of all the cURL parameters I usually use are:

-X [action]
Pretty straight forward... let's you specify an action (GET, POST, PUT, DELETE) to take on the URL.
> curl -X GET http://localhost:3000/sites
> curl -X GET http://localhost:3000/sites.xml
> curl -X DELETE http://localhost:3000/sites/1
-H [header]
This lets you specify your HTTP headers. I usually use Content-type and Accept here.
> curl -H "Accept: text/xml" -X GET http://localhost:3000/sites
-d [parameter]
Again, duh. Let's you specify your parameters to send to the URL - specifically for posting. Note that when using the -d flag, you don't need to specify POST as your action because it's the default. If you're using PUT though, you'll need to add -X PUT.
> curl -d "site[site]" -d "site[owner]=derek" http://localhost:3000/sites
> curl -H "Accept: text/xml" -d "site[site]" -d "site[owner]=derek" http://localhost:3000/sites
And here's how I use it most often - to POST an XML request to the web service and get an XML response back.
> curl -H "Accept: text/xml" -H "Content-type: application/xml" -d "<?xml version ='1.0' encoding 'UTF-8'?> <site><site></site><owner>derek</owner></site>" http://localhost:3000/sites

written by 26 year old Derek Brooks


glad you're doing this all day and not me

yawn posted

well it's either that or post anonymous comments on my blog every few weeks.

derek posted

have you noticed your time between blogs is getting longer and longer...

redd posted

Thanks! This post has been very helpful!

Willem posted

7 years later, this was a big help! cURL is an indispensable tool - and now I've got it playing nice with Rails. Thanks!

Dylan posted

won't be shared or displayed