README.md 11.5 KB
Newer Older
1
# ![http.rb](https://raw.github.com/httprb/http.rb/master/logo.png)
2

3 4
[![Gem Version](https://badge.fury.io/rb/http.svg)](https://rubygems.org/gems/http)
[![Build Status](https://secure.travis-ci.org/httprb/http.svg?branch=master)](https://travis-ci.org/httprb/http)
5 6
[![Code Climate](https://codeclimate.com/github/httprb/http.svg?branch=master)](https://codeclimate.com/github/httprb/http)
[![Coverage Status](https://coveralls.io/repos/httprb/http/badge.svg?branch=master)](https://coveralls.io/r/httprb/http)
7
[![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/httprb/http/blob/master/LICENSE.txt)
8

9
## About
10

11 12 13
HTTP (The Gem! a.k.a. http.rb) is an easy-to-use client library for making requests
from Ruby. It uses a simple method chaining system for building requests, similar to
Python's [Requests].
14

15 16 17 18
Under the hood, http.rb uses [http_parser.rb], a fast HTTP parsing native
extension based on the Node.js parser and a Java port thereof. This library
isn't just yet another wrapper around Net::HTTP. It implements the HTTP protocol
natively and outsources the parsing to native extensions.
19 20 21 22 23

[requests]: http://docs.python-requests.org/en/latest/
[http_parser.rb]: https://github.com/tmm1/http_parser.rb


24
## Another Ruby HTTP library? Why should I care?
25

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
There are a lot of HTTP libraries to choose from in the Ruby ecosystem.
So why would you choose this one?

Top three reasons:

1. **Clean API**: http.rb offers an easy-to-use API that should be a
   breath of fresh air after using something like Net::HTTP.

2. **Maturity**: http.rb is one of the most mature Ruby HTTP clients, supporting
   features like persistent connections and fine-grained timeouts.

3. **Performance**: using native parsers and a clean, lightweight implementation,
   http.rb achieves the best performance of any Ruby HTTP library which
   implements the HTTP protocol in Ruby instead of C:

  | HTTP client              | time      |
  |--------------------------|-----------|
  | curb (persistent)        | 2.519088  |
  | em-http-request          | 2.731645  |
  | Typhoeus                 | 2.851911  |
  | StreamlyFFI (persistent) | 2.853786  |
  | http.rb (persistent)     | 2.970702  |
  | http.rb                  | 3.588964  |
  | HTTParty                 | 3.931913  |
  | Net::HTTP                | 3.959342  |
  | Net::HTTP (persistent)   | 4.043674  |
  | open-uri                 | 4.479817  |
  | Excon (persistent)       | 4.618361  |
  | Excon                    | 4.701262  |
  | RestClient               | 26.832668 |

Benchmarks performed using excon's benchmarking tool

59 60 61 62
DISCLAIMER: Most benchmarks you find in READMEs are crap,
including this one. These are out-of-date. If you care about
performance, benchmark for yourself for your own use cases!

63 64 65 66 67 68 69 70 71 72
## Help and Discussion

If you need help or just want to talk about the http.rb,
visit the http.rb Google Group:

https://groups.google.com/forum/#!forum/httprb

You can join by email by sending a message to:

[httprb+subscribe@googlegroups.com](mailto:httprb+subscribe@googlegroups.com)
73 74 75

If you believe you've found a bug, please report it at:

76
https://github.com/httprb/http/issues
77

78 79

## Installation
80 81 82

Add this line to your application's Gemfile:

83
    gem "http"
84 85 86 87 88 89 90 91 92 93 94

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install http

Inside of your Ruby program do:

95
    require "http"
96 97 98

...to pull it in as a dependency.

99

100 101 102
## Documentation

[Please see the http.rb wiki](https://github.com/httprb/http/wiki)
103
for more detailed documentation and usage notes.
104

105 106

## Basic Usage
107 108 109

Here's some simple examples to get you started:

110

111
### GET requests
112 113

```ruby
114
>> HTTP.get("https://github.com").to_s
115 116 117
=> "<html><head><meta http-equiv=\"content-type\" content=..."
```

118 119
That's all it takes! To obtain an `HTTP::Response` object instead of the response
body, all we have to do is omit the #to_s on the end:
120 121

```ruby
122
>> HTTP.get("https://github.com")
123
=> #<HTTP/1.0 200 OK @headers={"Content-Type"=>"text/html; charset=UTF-8", "Date"=>"Fri, ...>
124
 => #<HTTP::Response/1.1 200 OK @headers={"Content-Type"=>"text/html; ...>
125 126
```

127
We can also obtain an `HTTP::Response::Body` object for this response:
128 129

```ruby
130 131
>> HTTP.get("https://github.com").body
 => #<HTTP::Response::Body:814d7aac @streaming=false>
132 133
```

134
The response body can be streamed with `HTTP::Response::Body#readpartial`:
135 136

```ruby
137
>> HTTP.get("https://github.com").body.readpartial
138 139 140
 => "<!doctype html><html "
```

141 142 143
In practice you'll want to bind the HTTP::Response::Body to a local variable (e.g.
"body") and call readpartial on it repeatedly until it returns `nil`.

144 145 146

### POST requests

147 148 149
Making POST requests is simple too. Want to POST a form?

```ruby
150
HTTP.post("http://example.com/resource", :form => {:foo => "42"})
151 152 153 154
```
Making GET requests with query string parameters is as simple.

```ruby
155
HTTP.get("http://example.com/resource", :params => {:foo => "bar"})
156 157 158 159 160
```

Want to POST with a specific body, JSON for instance?

```ruby
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
HTTP.post("http://example.com/resource", :json => { :foo => "42" })
```

Or just a plain body?

```ruby
HTTP.post("http://example.com/resource", :body => "foo=42&bar=baz")
```

Posting a file?

``` ruby
HTTP.post("http://examplc.com/resource", :form => {
  :username => "ixti",
  :avatar   => HTTP::FormData::File.new("/home/ixit/avatar.png")
})
177 178 179 180 181 182 183 184
```

It's easy!


### Proxy Support

Making request behind proxy is as simple as making them directly. Just specify
185
hostname (or IP address) of your proxy server and its port, and here you go:
186 187 188

```ruby
HTTP.via("proxy-hostname.local", 8080)
189
  .get("http://example.com/resource")
190 191
```

192
Proxy needs authentication? No problem:
193 194

```ruby
195
HTTP.via("proxy-hostname.local", 8080, "username", "password")
196
  .get("http://example.com/resource")
197 198 199
```


200
### Adding Headers
201

202
The HTTP gem uses the concept of chaining to simplify requests. Let's say
203
you want to get the latest commit of this library from GitHub in JSON format.
204 205 206
One way we could do this is by tacking a filename on the end of the URL:

```ruby
207
HTTP.get("https://github.com/httprb/http/commit/HEAD.json")
208 209
```

210
The GitHub API happens to support this approach, but really this is a bit of a
211 212 213 214 215 216
hack that makes it easy for people typing URLs into the address bars of
browsers to perform the act of content negotiation. Since we have access to
the full, raw power of HTTP, we can perform content negotiation the way HTTP
intends us to, by using the Accept header:

```ruby
217 218
HTTP.headers(:accept => "application/json")
  .get("https://github.com/httprb/http/commit/HEAD")
219 220
```

221 222
This requests JSON from GitHub. GitHub is smart enough to understand our
request and returns a response with `Content-Type: application/json`.
223

224
Shorter alias exists for `HTTP.headers`:
225 226

```ruby
227 228 229 230
HTTP[:accept => "application/json"]
  .get("https://github.com/httprb/http/commit/HEAD")
```

231

232 233 234 235 236 237 238 239
### Authorization Header

With [HTTP Basic Authentication](http://tools.ietf.org/html/rfc2617) using
a username and password:

```ruby
HTTP.basic_auth(:user => "user", :pass => "pass")
# <HTTP::Headers {"Authorization"=>"Basic dXNlcjpwYXNz"}>
240 241
```

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
Or with plain as-is value:

```ruby
HTTP.auth("Bearer VGhlIEhUVFAgR2VtLCBST0NLUw")
# <HTTP::Headers {"Authorization"=>"Bearer VGhlIEhUVFAgR2VtLCBST0NLUw"}>
```

And Chain all together!

```ruby
HTTP.basic_auth(:user => "user", :pass => "pass")
  .headers("Cookie" => "9wq3w")
  .get("https://example.com")
```


258
### Content Negotiation
259

260
As important a concept as content negotiation is to HTTP, it sure should be easy,
261 262 263 264
right? But usually it's not, and so we end up adding ".json" onto the ends of
our URLs because the existing mechanisms make it too hard. It should be easy:

```ruby
265
HTTP.accept(:json).get("https://github.com/httprb/http/commit/HEAD")
266 267 268 269 270
```

This adds the appropriate Accept header for retrieving a JSON response for the
given resource.

271

272 273
### Reuse HTTP connection: HTTP Keep-Alive

274 275
If you need to make many successive requests against the same host,
you can create client with persistent connection to the host:
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
``` ruby
begin
  # create HTTP client with persistent connection to api.icndb.com:
  http = HTTP.persistent "http://api.icndb.com"

  # issue multiple requests using same connection:
  jokes = 100.times.map { http.get("/jokes/random").to_s }
ensure
  # close underlying connection when you don't need it anymore
  http.close if http
end

```

If the optional code block is given, it will be passed the client with
persistent connection to the host as an argument and `client.close` will be
automatically called when the block terminates.
The value of the block will be returned:

``` ruby
jokes = HTTP.persistent "http://api.icndb.com" do |http|
  100.times.map { http.get("/jokes/random").to_s }
299 300
end
```
301

302
##### NOTICE
303

304 305 306 307
You must consume response before sending next request via persistent connection.
That means you need to call `#to_s`, `#parse` or `#flush` on response object.
In the example above we used `http.get("/jokes/random").to_s` to get response
bodies. That works perfectly fine, because `#to_s` reads off the response.
308

309 310 311 312
Sometimes you don't need response body, or need whole response object to
access it's status, headers etc instead. You can either call `#to_s` to
make sure response was flushed and then use response object itself, or use
`#flush` (syntax sugar for `#tap(&:to_s)` that will do that for you:
313 314


315 316 317 318
``` ruby
contents = HTTP.persistent "http://en.wikipedia.org" do |http|
  %w(Hypertext_Transfer_Protocol Git GitHub Linux Hurd).map do
    http.get("/wiki/#{target}").flush
319 320 321 322 323
  end
end
```


324
### Timeouts
325

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
By default, HTTP does not timeout on a request. You can enable per operation
(each read/write/connect call) or global (sum of all read/write/connect calls).

Per operation timeouts are what `Net::HTTP` and the majority of HTTP clients do:

``` ruby
HTTP.timeout(:per_operation, :write => 2, :connect => 5, :read => 10)
  .get "http://example.com"

# For convinience, you can omit timeout type in this case. So following has
# same result as the above:

HTTP.timeout(:write => 2, :connect => 5, :read => 10).get "http://example.com"
```

Global timeouts let you set an upper bound of how long a request can take,
without having to rely on `Timeout.timeout`:

``` ruby
HTTP.timeout(:global, :write => 1, :connect => 1, :read => 1)
  .get "http://example.com"
```

Uses a timeout of 3 seconds, for the entire `get` call.

*Warning!* You cannot use Celluloid::IO with timeouts currently.


## Supported Ruby Versions
355 356 357 358 359 360

This library aims to support and is [tested against][travis] the following Ruby
versions:

* Ruby 1.9.3
* Ruby 2.0.0
361 362
* Ruby 2.1.x
* Ruby 2.2.x
363
* Ruby 2.3.x
364
* JRuby 1.7.x
365
* JRuby 9000+
366 367 368 369 370 371 372 373 374 375 376 377 378 379

If something doesn't work on one of these versions, it's a bug.

This library may inadvertently work (or seem to work) on other Ruby versions,
however support will only be provided for the versions listed above.

If you would like this library to support another Ruby version or
implementation, you may volunteer to be a maintainer. Being a maintainer
entails making sure all tests run and pass on that implementation. When
something breaks on your implementation, you will be responsible for providing
patches in a timely fashion. If critical issues for a particular implementation
exist at the time of a major release, support for that Ruby version may be
dropped.

380
[travis]: http://travis-ci.org/httprb/http
381 382


383
## Contributing to http.rb
384

385 386 387 388
* Fork http.rb on GitHub
* Make your changes
* Ensure all tests pass (`bundle exec rake`)
* Send a pull request
389 390
* If we like them we'll merge them
* If we've accepted a patch, feel free to ask for commit access!
391 392


393 394
## Copyright

395
Copyright (c) 2011-2016 Tony Arcieri, Erik Michaels-Ober, Alexey V. Zapparov, Zachary Anker.
396
See LICENSE.txt for further details.