I’ve begun experimenting with the new Edge Rails code that includes built-in support for REST. (The code that was once in the simply_restful plugin is now a part of Edge Rails.) With the capability to generate multiple types of output per action, it has become more and more useful to test actions on the command-line using cURL. Here are some useful cURL parameters I’ve used:

  • -X [action]: Allows you to specify an HTTP action such as GET, POST, PUT or DELETE.
    Example:

    curl -X DELETE http://localhost:3000/books/1
  • -d [parameter]: Lets you set variables as if they were POSTed in a form to the URL. Note that this automatically makes the request a POST HTTP action type (no -X necessary).
    Example:

    curl -d "book[title]=Test" -d "book[copyright]=1998"
    http://localhost:3000/books
  • -H [header]: Gives you the option of setting an HTTP header such as Content-Type or Accept. This is particularly useful for requesting text/xml as the Accept type.
    Example:

    curl -H "Accept: text/xml"
    http://localhost:3000/books/sections/1

Putting it all together, here’s a cURL command that updates the title of a Book object using the PUT action and expects an xml response:

curl -H "Accept: text/xml" -X PUT -d "book[title]=Testing Again"
http://localhost:3000/books/1