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.
This lets you specify your HTTP headers. I usually use Content-type and Accept here.
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.
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/sitesAnd 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