Convert Figma logo to code with AI

taf2 logocurb

Ruby bindings for libcurl

1,299
230
1,299
19

Top Related Projects

Typhoeus wraps libcurl in order to make fast and reliable requests.

5,813

Simple, but flexible HTTP client library, with support for multiple backends.

:tada: Makes http fun again!

Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.

Quick Overview

Curb is a Ruby extension that provides bindings to the libcurl library. It allows Ruby developers to easily make HTTP requests and interact with various network protocols, offering a powerful and flexible alternative to Ruby's built-in Net::HTTP library.

Pros

  • High performance due to its C implementation and use of libcurl
  • Supports multiple protocols including HTTP, HTTPS, FTP, and more
  • Offers advanced features like multi-threading and parallel requests
  • Provides a familiar and Ruby-like API

Cons

  • Requires compilation of native extensions, which can be challenging on some systems
  • May have compatibility issues with certain Ruby versions or platforms
  • Less widely used compared to other HTTP libraries in the Ruby ecosystem
  • Documentation could be more comprehensive and up-to-date

Code Examples

  1. Making a simple GET request:
require 'curb'

http = Curl.get("https://api.example.com/data")
puts http.body_str
  1. Performing a POST request with data:
http = Curl::Easy.new("https://api.example.com/create")
http.http_post(
  Curl::PostField.content('name', 'John Doe'),
  Curl::PostField.content('email', 'john@example.com')
)
puts http.body_str
  1. Handling HTTP basic authentication:
http = Curl::Easy.new("https://api.example.com/secure")
http.http_auth_types = :basic
http.username = 'user'
http.password = 'password'
http.perform
puts http.body_str

Getting Started

To use Curb in your Ruby project, follow these steps:

  1. Install the gem:

    gem install curb
    
  2. In your Ruby file, require the library:

    require 'curb'
    
  3. Make a simple request:

    response = Curl.get("https://api.example.com")
    puts response.body_str
    

Now you're ready to use Curb for your HTTP requests in Ruby!

Competitor Comparisons

Typhoeus wraps libcurl in order to make fast and reliable requests.

Pros of Typhoeus

  • Built-in support for parallel HTTP requests, allowing for faster execution of multiple requests
  • More extensive feature set, including advanced options like request queueing and automatic retries
  • Active development and maintenance, with regular updates and improvements

Cons of Typhoeus

  • Steeper learning curve due to more complex API and additional features
  • Potentially higher memory usage, especially when handling large numbers of concurrent requests
  • May be overkill for simpler use cases or projects with minimal HTTP request needs

Code Comparison

Curb example:

http = Curl.get("http://example.com")
puts http.body_str

Typhoeus example:

response = Typhoeus.get("http://example.com")
puts response.body

While both libraries offer similar basic functionality, Typhoeus provides more advanced features for complex HTTP operations:

hydra = Typhoeus::Hydra.new
requests = 10.times.map { Typhoeus::Request.new("http://example.com") }
requests.each { |request| hydra.queue(request) }
hydra.run

This example demonstrates Typhoeus's ability to easily queue and execute multiple requests in parallel, a feature not readily available in Curb.

5,813

Simple, but flexible HTTP client library, with support for multiple backends.

Pros of Faraday

  • More flexible and adaptable to various HTTP libraries
  • Supports middleware for request/response processing
  • Actively maintained with regular updates

Cons of Faraday

  • Slightly more complex setup compared to Curb
  • May have a steeper learning curve for beginners
  • Potentially slower performance for simple HTTP requests

Code Comparison

Curb example:

http = Curl.get("http://example.com/")
puts http.body_str

Faraday example:

conn = Faraday.new(url: 'http://example.com')
response = conn.get('/')
puts response.body

Both Curb and Faraday are Ruby libraries for making HTTP requests, but they have different approaches. Curb is a thin wrapper around libcurl, providing direct access to its functionality. Faraday, on the other hand, is an HTTP client library that provides a common interface for making requests using various adapters.

Curb is generally simpler to use for basic HTTP requests and may offer better performance due to its direct use of libcurl. However, Faraday's flexibility and middleware support make it more suitable for complex scenarios and integration with different HTTP libraries.

The choice between Curb and Faraday depends on the specific requirements of your project, such as simplicity, flexibility, and performance needs.

:tada: Makes http fun again!

Pros of HTTParty

  • Simpler, more intuitive API for basic HTTP requests
  • Built-in JSON parsing and support for custom parsers
  • Extensive documentation and large community support

Cons of HTTParty

  • Less performant for high-volume requests compared to Curb
  • Limited support for advanced features like multi-threading and connection pooling
  • Fewer options for fine-grained control over HTTP connections

Code Comparison

HTTParty:

require 'httparty'

response = HTTParty.get('https://api.example.com/users')
puts response.body, response.code, response.message, response.headers.inspect

Curb:

require 'curb'

http = Curl.get("https://api.example.com/users")
puts http.body_str, http.response_code, http.header_str

HTTParty provides a more straightforward syntax for basic requests, while Curb offers more low-level control. HTTParty automatically parses JSON responses, whereas Curb requires manual parsing. Curb's approach is closer to the underlying libcurl library, providing more flexibility for advanced use cases.

Both libraries have their strengths, with HTTParty excelling in simplicity and ease of use, and Curb offering better performance and more granular control over HTTP operations. The choice between them depends on the specific requirements of your project, such as the need for advanced features, performance considerations, and the complexity of the HTTP interactions.

Simple HTTP and REST client for Ruby, inspired by microframework syntax for specifying actions.

Pros of rest-client

  • Pure Ruby implementation, making it more portable and easier to install
  • Supports various authentication methods out of the box
  • More actively maintained with frequent updates

Cons of rest-client

  • Generally slower performance compared to curb
  • Lacks some advanced features available in curb
  • May consume more memory for large requests

Code Comparison

rest-client:

require 'rest-client'
response = RestClient.get('https://api.example.com/data')
puts response.body

curb:

require 'curb'
http = Curl.get('https://api.example.com/data')
puts http.body_str

Both libraries provide simple ways to make HTTP requests, but curb's syntax is slightly more concise. rest-client offers a more Ruby-like interface, while curb provides a thin wrapper around libcurl.

rest-client is generally easier to use for beginners and offers more built-in features, making it suitable for most Ruby projects. curb, on the other hand, provides better performance and more low-level control, making it ideal for applications with specific performance requirements or those needing advanced HTTP functionality.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

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 https://curl.se/libcurl/ .

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

A big advantage to Curb over all other known ruby http libraries is it's ability to handle timeouts without the use of threads.

License

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

Easy mode

GET request

  res = Curl.get("https://www.google.com/") {|http|
    http.timeout = 10 # raise exception if request/response not handled within 10 seconds
  }
  puts res.code
  puts res.head
  puts res.body

POST request

  res = Curl.post("https://your-server.com/endpoint", {post: "this"}.to_json) {|http|
    http.headers["Content-Type"] = "application/json"
  }
  puts res.code
  puts res.head
  puts res.body

FTP Support

require 'curb'

Basic FTP Download

puts "=== FTP Download Example ==="
ftp = Curl::Easy.new('ftp://ftp.example.com/remote/file.txt')
ftp.username = 'user'
ftp.password = 'password'
ftp.perform
puts ftp.body

FTP Upload

puts "\n=== FTP Upload Example ==="
upload = Curl::Easy.new('ftp://ftp.example.com/remote/upload.txt')
upload.username = 'user'
upload.password = 'password'
upload.upload = true
upload.put_data = File.read('local_file.txt')
upload.perform

List Directory Contents

puts "\n=== FTP Directory Listing Example ==="
list = Curl::Easy.new('ftp://ftp.example.com/remote/directory/')
list.username = 'user'
list.password = 'password'
list.dirlistonly = true
list.perform
puts list.body

Advanced FTP Usage with Various Options

puts "\n=== Advanced FTP Example ==="
advanced = Curl::Easy.new do |curl|
  curl.url = 'ftp://ftp.example.com/remote/file.txt'
  curl.username = 'user'
  curl.password = 'password'

  # FTP Options
  curl.ftp_response_timeout = 30
  curl.ftp_create_missing_dirs = true   # Create directories if they don't exist
  curl.ftp_filemethod = Curl::CURL_MULTICWD  # Use multicwd method for traversing paths

  # SSL/TLS Options for FTPS
  curl.use_ssl = Curl::CURLUSESSL_ALL  # Use SSL/TLS for control and data
  curl.ssl_verify_peer = true
  curl.ssl_verify_host = true
  curl.cacert = "/path/to/cacert.pem"

  # Progress callback
  curl.on_progress do |dl_total, dl_now, ul_total, ul_now|
    puts "Download: #{dl_now}/#{dl_total} Upload: #{ul_now}/#{ul_total}"
    true # must return true to continue
  end

  # Debug output
  curl.verbose = true
  curl.on_debug do |type, data|
    puts "#{type}: #{data}"
    true
  end
end

advanced.perform

Parallel FTP Downloads

puts "\n=== Parallel FTP Downloads Example ==="
urls = [
  'ftp://ftp.example.com/file1.txt',
  'ftp://ftp.example.com/file2.txt',
  'ftp://ftp.example.com/file3.txt'
]

Common options for all connections

options = {
  :username => 'user',
  :password => 'password',
  :timeout => 30,
  :on_success => proc { |easy| puts "Successfully downloaded: #{easy.url}" },
  :on_failure => proc { |easy, code| puts "Failed to download: #{easy.url} (#{code})" }
}

Curl::Multi.download(urls, options) do |curl, file_path|
  puts "Completed downloading to: #{file_path}"
end

You will need

  • A working Ruby installation (2.0.0+ will work but 2.1+ preferred) (it's possible it still works with 1.8.7 but you'd have to tell me if not...)
  • A working libcurl development installation (Ideally one of the versions listed in the compatibility chart below that maps to your curb version)
  • A sane build environment (e.g. gcc, make)

Version Compatibility chart

A non-exhaustive set of compatibility versions of the libcurl library with this gem are as follows. (Note that these are only the ones that have been tested and reported to work across a variety of platforms / rubies)

Gem VersionRelease Datelibcurl versions
1.0.8Feb 10, 20257.58 – 8.12.1
1.0.7Feb 09, 20257.58 – 8.12.1
1.0.6Aug 23, 20247.58 – 8.12.1
1.0.5Jan 20237.58 – 8.12.1
1.0.4Jan 20237.58 – 8.12.1
1.0.3*Dec 20227.58 – 8.12.1
1.0.2*Dec 20227.58 – 8.12.1
1.0.1Apr 20227.58 – 8.12.1
1.0.0Jan 20227.58 – 8.12.1
0.9.8Jan 20197.58 – 7.81
0.9.7Nov 20187.56 – 7.60
0.9.6May 20187.51 – 7.59
0.9.5May 20187.51 – 7.59
0.9.4Aug 20177.41 – 7.58
0.9.3Apr 20167.26 – 7.58

*avoid using these version are known to have issues with segmentation faults

Installation...

... will usually be as simple as:

$ gem install curb

On Windows, make sure you're using the DevKit and the development version of libcurl. Unzip, then run this in your command line (alter paths to your curl location, but remember to use forward slashes):

gem install curb --platform=ruby -- --with-curl-lib=C:/curl-7.39.0-devel-mingw32/lib --with-curl-include=C:/curl-7.39.0-devel-mingw32/include

Note that with Windows moving from one method of compiling to another as of Ruby 2.4 (DevKit -> MYSYS2), the usage of Ruby 2.4+ with this gem on windows is unlikely to work. It is advised to use the latest version of Ruby 2.3 available HERE

Or, if you downloaded the archive:

$ rake compile && rake install

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

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

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 http://github.com/taf2/curb/issues

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

18.04 and onwards

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

< 18.04

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

On RedHat:

$ sudo yum install ruby-devel libcurl-devel openssl-devel

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.

To use either, you will need to require the curb gem:

require 'curb'

Super simple API (less typing)

http = Curl.get("http://www.google.com/")
puts http.body

http = Curl.post("http://www.google.com/", {:foo => "bar"})
puts http.body

http = Curl.get("http://www.google.com/") do |http|
  http.headers['Cookie'] = 'foo=1;bar=2'
end
puts http.body

Simple fetch via HTTP:

c = Curl::Easy.perform("http://www.google.co.uk")
puts c.body

Same thing, more manual:

c = Curl::Easy.new("http://www.google.co.uk")
c.perform
puts c.body

Additional config:

http = Curl::Easy.perform("http://www.google.co.uk") do |curl|
  curl.headers["User-Agent"] = "myapp-0.0"
  curl.verbose = true
end

Same thing, more manual:

c = Curl::Easy.new("http://www.google.co.uk") do |curl|
  curl.headers["User-Agent"] = "myapp-0.0"
  curl.verbose = true
end

c.perform

HTTP basic authentication:

c = Curl::Easy.new("http://github.com/")
c.http_auth_types = :basic
c.username = 'foo'
c.password = 'bar'
c.perform

HTTP "insecure" SSL connections (like curl -k, --insecure) to avoid Curl::Err::SSLCACertificateError:

c = Curl::Easy.new("https://github.com/")
c.ssl_verify_peer = false
c.perform

Supplying custom handlers:

c = Curl::Easy.new("http://www.google.co.uk")

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

c.perform

Reusing Curls:

c = Curl::Easy.new

["http://www.google.co.uk", "http://www.ruby-lang.org/"].map do |url|
  c.url = url
  c.perform
  c.body
end

HTTP POST form:

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

HTTP POST file upload:

c = Curl::Easy.new("http://my.rails.box/files/upload")
c.multipart_form_post = true
c.http_post(Curl::PostField.file('thing[file]', 'myfile.rb'))

Using HTTP/2

c = Curl::Easy.new("https://http2.akamai.com")
c.set(:HTTP_VERSION, Curl::HTTP_2_0)

c.perform
puts (c.body.include? "You are using HTTP/2 right now!") ? "HTTP/2" : "HTTP/1.x"

Multi Interface (Basic HTTP GET):

# make multiple GET requests
easy_options = {:follow_location => true}
# Use Curl::CURLPIPE_MULTIPLEX for HTTP/2 multiplexing
multi_options = {:pipeline => Curl::CURLPIPE_HTTP1}

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
end

Multi Interface (Basic HTTP POST):

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


url_fields = [
  { :url => 'url1', :post_fields => {'f1' => 'v1'} },
  { :url => 'url2', :post_fields => {'f1' => 'v1'} },
  { :url => 'url3', :post_fields => {'f1' => 'v1'} }
]

Curl::Multi.post(url_fields, easy_options, multi_options) do|easy|
  # do something interesting with the easy response
  puts easy.last_effective_url
end

Multi Interface (Advanced):

responses = {}
requests = ["http://www.google.co.uk/", "http://www.ruby-lang.org/"]
m = Curl::Multi.new
# add a few easy handles
requests.each do |url|
  responses[url] = ""
  c = Curl::Easy.new(url) 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" }
  end
  m.add(c)
end

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

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

Easy Callbacks

  • on_success is called when the response code is 2xx
  • on_redirect is called when the response code is 3xx
  • on_missing is called when the response code is 4xx
  • on_failure is called when the response code is 5xx
  • on_complete is called in all cases.