davideisinger.com

My personal website
Log | Files | Refs | README

index.md (3353B)


      1 ---
      2 title: "cURL and Your Rails 2 App"
      3 date: 2008-03-28T00:00:00+00:00
      4 draft: false
      5 canonical_url: https://www.viget.com/articles/curl-and-your-rails-2-app/
      6 ---
      7 
      8 If you're anything like me, you've used
      9 [cURL](https://en.wikipedia.org/wiki/CURL) to download a batch of MP3
     10 files from the web, or to move a TAR file from one remote server to
     11 another. It might come as a surprise, then, that cURL is a full-featured
     12 HTTP client, which makes it perfect for interacting with RESTful web
     13 services like the ones encouraged by Rails 2. To illustrate, let's
     14 create a small Rails app called `tv_show`:
     15 
     16 ```sh
     17 rails tv_show
     18 cd tv_show
     19 script/generate scaffold character name:string action:string
     20 rake db:migrate
     21 script/server
     22 ```
     23 
     24 Fire up your web browser and create a few characters. Once you've done
     25 that, open a new terminal window and try the following:
     26 
     27 ```
     28 curl http://localhost:3000/characters.xml
     29 ```
     30 
     31 You'll get a nice XML representation of your characters:
     32 
     33 ```xml
     34 <?xml version"1.0" encoding="UTF-8"?>
     35 <characters type="array">
     36     <character>
     37         <id type="integer">1</id>
     38         <name>George Sr.</name>
     39         <action>goes to jail</action>
     40         <created-at type="datetime">2008-03-28T11:01:57-04:00</created-at>
     41         <updated-at type="datetime">2008-03-28T11:01:57-04:00</updated-at>
     42     </character>
     43     <character>
     44         <id type="integer">2</id>
     45         <name>Gob</name>
     46         <action>rides a Segway</action>
     47         <created-at type="datetime">2008-03-28T11:02:07-04:00</created-at>
     48         <updated-at type="datetime">2008-03-28T11:02:12-04:00</updated-at>
     49     </character>
     50     <character>
     51         <id type="integer">3</id>
     52         <name>Tobias</name>
     53         <action>wears cutoffs</action>
     54         <created-at type="datetime">2008-03-28T11:02:20-04:00</created-at>
     55         <updated-at type="datetime">2008-03-28T11:02:20-04:00</updated-at>
     56     </character>
     57 </characters>
     58 ```
     59 
     60 You can retrieve the representation of a specific character by
     61 specifying his ID in the URL:
     62 
     63 ```sh
     64 curl http://localhost:3000/characters/1.xml
     65 ```
     66 
     67 ```xml
     68 <?xml version="1.0" encoding="UTF-8"?>
     69 <character>
     70     <id type="integer">1</id>
     71     <name>George Sr.</name>
     72     <action>goes to jail</action>
     73     <created-at type="datetime">2008-03-28T11:01:57-04:00</created-at>
     74     <updated-at type="datetime">2008-03-28T11:01:57-04:00</updated-at>
     75 </character>
     76 ```
     77 
     78 To create a new character, issue a POST request, use the -X flag to
     79 specify the action, and the -d flag to define the request body:
     80 
     81 ```sh
     82 curl -X POST -d "character[name]=Lindsay&character[action]=does+nothing" http://localhost:3000/characters.xml
     83 ```
     84 
     85 Here's where things get interesting: unlike most web browsers, which
     86 only support GET and POST, cURL supports the complete set of HTTP
     87 actions. If we want to update one of our existing characters, we can
     88 issue a PUT request to the URL of that character's representation, like
     89 so:
     90 
     91 ```sh
     92 curl -X PUT -d "character[action]=works+at+clothing+store" http://localhost:3000/characters/4.xml
     93 ```
     94 
     95 If we want to delete a character, issue a DELETE request:
     96 
     97 ```sh
     98 curl -X DELETE http://localhost:3000/characters/1.xml
     99 ```
    100 
    101 For some more sophisticated uses of REST and Rails, check out
    102 [rest-client](https://rest-client.heroku.com/rdoc/) and
    103 [ActiveResource](http://ryandaigle.com/articles/2006/06/30/whats-new-in-edge-rails-activeresource-is-here).