Skip to content
This repository
tree: dceafcd931
Fetching contributors…

Cannot retrieve contributors at this time

file 194 lines (133 sloc) 5.478 kb

Curb - Libcurl bindings for Ruby

Curb (probably CUrl-RuBy or something) provides Ruby-language bindings for the libcurl(3), a fully-featured client-side URL transfer library. cURL and libcurl live at .

Curb is a work-in-progress, and currently only supports libcurl's 'easy' and 'multi' modes.


Curb is copyright (c)2006 Ross Bamford, and released under the terms of the Ruby license. See the LICENSE file for the gory details.

You will need

  • A working Ruby installation (1.8+, tested with 1.8.6, 1.8.7, 1.9.1, and 1.9.2)
  • A working (lib)curl installation, with development stuff (7.5+, tested with 7.19.x)
  • A sane build environment (e.g. gcc, make)


... will usually be as simple as:

$ gem install curb

Or, if you downloaded the archive:

$ rake install 

If you have a wierd setup, you might need extconf options. In this case, pass them like so:

$ rake install EXTCONF_OPTS='--with-curl-dir=/path/to/libcurl --prefix=/what/ever'

Curb is tested only on GNU/Linux x86 and Mac OSX - YMMV on other platforms. If you do use another platform and experience problems, or if you can expand on the above instructions, please report the issue at

On Ubuntu, the dependencies can be satisfied by installing the following packages:

$ sudo apt-get install libcurl3 libcurl3-gnutls libcurl4-openssl-dev

Curb has fairly extensive RDoc comments in the source. You can build the documentation with:

$ rake doc

Usage & examples

Curb provides two classes:

  • Curl::Easy - simple API, for day-to-day tasks.
  • Curl::Multi - more advanced API, for operating on multiple URLs simultaneously.

Super simple API (less typing)

http = Curl.get("")
puts http.body_str

http ="", {:foo => "bar"})
puts http.body_str

http = Curl.get("") do|http|
  http.headers['Cookie'] = 'foo=1;bar=2'
puts http.body_str

Simple fetch via HTTP:

c = Curl::Easy.perform("")
puts c.body_str

Same thing, more manual:

c ="")
puts c.body_str

Additional config:

Curl::Easy.perform("") do |curl| 
  curl.headers["User-Agent"] = "myapp-0.0"
  curl.verbose = true

Same thing, more manual:

c ="") do |curl| 
  curl.headers["User-Agent"] = "myapp-0.0"
  curl.verbose = true


HTTP basic authentication:

c ="")
c.http_auth_types = :basic
c.username = 'foo'
c.password = 'bar'

Supplying custom handlers:

c ="")

c.on_body { |data| print(data) }
c.on_header { |data| print(data) }


Reusing Curls:

c =

["", ""].map do |url|
  c.url = url


c = Curl::Easy.http_post("",
                         Curl::PostField.content('thing[name]', 'box'),
                         Curl::PostField.content('thing[type]', 'storage'))

HTTP POST file upload:

c ="")
c.multipart_form_post = true
c.http_post(Curl::PostField.file('thing[file]', 'myfile.rb'))

Multi Interface (Basic HTTP GET):

# make multiple GET requests
easy_options = {:follow_location => true}
multi_options = {:pipeline => true}

Curl::Multi.get('url1','url2','url3','url4','url5', easy_options, multi_options) do|easy|
  # do something interesting with the easy response
  puts easy.last_effective_url

Multi Interface (Basic HTTP POST):

# make multiple POST requests
easy_options = {:follow_location => true, :multipart_form_post => true}
multi_options = {:pipeline => true}

url_fields = [
  { :url => 'url1', :post_fields => {'f1' => 'v1'} },
  { :url => 'url2', :post_fields => {'f1' => 'v1'} },
  { :url => 'url3', :post_fields => {'f1' => 'v1'} }
], easy_options, multi_options) do|easy|
  # do something interesting with the easy response
  puts easy.last_effective_url

Multi Interface (Advanced):

responses = {}
requests = ["", ""]
m =
# add a few easy handles
requests.each do |url|
  responses[url] = ""
  c = do|curl|
    curl.follow_location = true
    curl.on_body{|data| responses[url] << data; data.size }
    curl.on_success {|easy| puts "success, add more easy handles" }

m.perform do
  puts "idling... can do some work here"

requests.each do|url|
  puts responses[url]

Easy Callbacks

on_success: is called when the response code is 20x on_failure: is called when the response code is not success, including redirects e.g. 30x on_complete: is called in all cases.

Something went wrong with that request. Please try again.