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]=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
  1. *yawn*

    glad you're doing this all day and not me

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

  3. Africa 2 years ago
  4. wtf dude, wtf...

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

  6. rwerwerewewe

  7. Thanks! This post has been very helpful!

    Willem 12 months ago

Your Comment

name
email (won't be displayed)
url
remember me

Sorry, this article has been known to get spammed, and the best way to curb it is by adding this random image below. Prove you're a real live human by typing the text from the image into the text box below.


text from image

Date

2 years ago (Feb 25, 2008 @ 12:53 pm)

Tags

Tech

Related Ads