January 05, 2009

Posted by John

Tagged httparty

Older: Look at the Size of My Footer

Newer: My Setup and Software

HTTParty Goes Commando

Hot of the press is a new feature for HTTParty, everyone’s favorite way of consuming sane web services. Thanks to Alex Vollmer, the party has been extended to your terminal with a sexy command line interface!

The CLI is available in 0.2.5, which should be on GitHub and RubyForge by the time you read this. If you like the addition, be sure to send Alex (@alexvollmer) a tweet to let him know and thank him. Enough with the gratitude, give me some examples you say!

Installation

# GitHub
$ sudo gem install jnunemaker-httparty --source http://gems.github.com

# RubyForge
$ sudo gem install httparty

Examples

I use Twitter for a lot of examples, so how about we start with the Twitter public timeline.

$ httparty http://twitter.com/statuses/public_timeline.json
[{"user"=>
   {"name"=>"sfwineboy",
    "url"=>"http://www.new.facebook.com/profile.php?id=1401240105",
    "id"=>"15651761",
    "description"=>"Chill - Axin",
    "protected"=>false,
    "screen_name"=>"sfwineboy",
    "followers_count"=>8,
    "location"=>"Los Angeles, CA",
    "profile_image_url"=>
     "http://s3.amazonaws.com/twitter_production/profile_images/57533829/a_281078039762fd87b263a939b652626f_normal.jpg"},
  "favorited"=>false,
  "truncated"=>false,
  "text"=>"Chillin in LA in Mar Vista http://loopt.us/VVQu2Q",
  "id"=>"1096656558",
  "in_reply_to_status_id"=>nil,
  "in_reply_to_user_id"=>nil,
  "source"=>"<a href=\"https://loopt.com\">Loopt</a>",
  "created_at"=>"Mon Jan 05 07:10:43 +0000 2009"},
  ...
}]

By default, the command line interface to HTTParty gives you a ruby pretty printed object. It doesn’t stop there though. If you want, you can format the output using the JSON gem’s pretty_generate version of JSON or REXML’s attempt at pretty indented xml.

$ httparty -f json http://twitter.com/statuses/public_timeline.json
[
  {
    "user": {
      "name": "Katrin Verclas",
      "url": "http:\/\/www.mobileactive.org",
      "id": "3125721",
      "description": "chica. curious.",
      "protected": false,
      "screen_name": "Katrinskaya",
      "followers_count": 935,
      "location": "",
      "profile_image_url": "http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/65202240\/Photo_95_normal.jpg"
    },
    "favorited": false,
    "truncated": false,
    "text": "It is 2 am and insomnia girl is trying hard not to get completely irritated at this shit. Drugs, baby, drugs.",
    "id": "1096661586",
    "in_reply_to_status_id": null,
    "in_reply_to_user_id": null,
    "source": "<a href=\"http:\/\/www.naan.net\/trac\/wiki\/TwitterFon\">TwitterFon<\/a>",
    "created_at": "Mon Jan 05 07:15:38 +0000 2009"
  },
  ...
 }
]

$ httparty -f xml "http://whoismyrepresentative.com/whoismyrep.php?zip=46544&output=xml"
<result n='1'>
  <rep name='Joe Donnelly' district='2' office='1218 Longworth' phone='(202) 225-3915' link='http://donnelly.house.gov/' state='IN'/>
</result>

You can also use different request methods, such as post and set headers and junk like that. See the help for more information.

$ httparty -h
USAGE: /opt/local/bin/httparty [options] [url]
    -f, --format [FORMAT]            Body format: plain, json or xml
    -r, --ruby                       Dump output in Ruby pretty-print format
    -a, --action [ACTION]            HTTP action: get (default), post, put or delete
    -d, --data [BODY]                Data to put in request body (prefix with '@' for file)
    -H, --header [NAME=VALUE]        Additional HTTP headers in NAME=VALUE form
    -v, --verbose                    If set, print verbose output
    -h, --help                       Show help documentation

I think this addition is pretty hot and I’m excited to actually need to try it out. This is, I’m sure, just the first pass of what will be more updates to the CLI of HTTParty, but I can see this completely replacing curl and your browser for testing API’s that you are building (or wrapping). Very cool stuff and much thanks to Alex.

9 Comments

  1. Thanks, this will be a really handy way for me to test API calls to Convio at work (this is our constituent management system).

  2. @akahn – Cool. Let me know if you get ideas for additions.

  3. That’s a very useful addition. Makes it much easier to quickly build out code against an API one isn’t too familiar with…

  4. Kevin Marsh Kevin Marsh

    Jan 05, 2009

    This is really cool. I can see it replacing curl like you say in very short order.

    I’m wondering about piping HTTParties together (bar hopping?), kind of like how Yahoo! Pipes works. It seems like a good idea, but I can’t think of any compelling use cases at the moment.

  5. Brandon Brandon

    Jan 08, 2009

    The dependency of json 1.1.3 stops Windows users from being able to use HTTParty (the most recent Windows json gem on rubyforge is 1.1.1) without modification. Any ideas?

  6. I suppose I could use json pure instead. I’ll look into it when I get a chance.

  7. Brandon Brandon

    Jan 08, 2009

    I changed the json dependency version 1.1.1 in my local gem to try out HTTParty on my Windows machine. It seems to work fine for some basic stuff I have had time to try. json_pure may be a good route, I have never really worked with it to know though. At the company I work for we develop on Windows and deploy on Ubuntu (Ruby on Rails apps anyways).

  8. I’m wondering if httparty is a good fit or a poor fit for downloading a large (around a gigabyte, say) file over http to save to the local filesystem. Would it be better to use something like curb for that? Does it matter much?

    Interested in people’s thoughts on this, and what the best tool for the job might be.

  9. json 1.1.3 not being available on windows is quite annoying.

    If a move to json pure fixes this, I’d be very grateful!

Sorry, comments are closed for this article to ease the burden of pruning spam.

About

Authored by John Nunemaker (Noo-neh-maker), a web developer and programmer who has fallen deeply in love with Ruby. More about John.

Syndication

Feed IconRailsTips Articles - An assortment of howto's and thoughts on Ruby and Rails.

Feed IconRails Quick Tips - Ruby and Rails related links.