Feb 25, 2008 12:53 PMUsing 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]=broox.com" -d "site[owner]=derek" http://localhost:3000/sites
> curl -H "Accept: text/xml" -d "site[site]=broox.com" -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>broox.com</site><owner>derek</owner></site>" http://localhost:3000/sites


Tags: tech

Comments

yawn

*yawn*

glad you're doing this all day and not me

4 months ago

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

4 months ago

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

4 months ago

Your Comment

name
email (won't be displayed)
url
Save my Information on this computer.
comment
Sorry, this article gets spammed a lot, and the best way to curb it is by
adding this random image below. Please type the text from the image into
the text box below it.
text from image