README.md 6.58 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
_NOTE: This is the 3.x **stable** branch.  For the 4.x **development** branch, please see:_
10 11 12

https://github.com/httprb/http/

13

14
## About
15

16 17 18
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].
19

20 21 22 23
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.
24 25 26 27 28

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


29
## Another Ruby HTTP library? Why should I care?
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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:

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  | HTTP client              | Time   | Implementation        |
  |--------------------------|--------|-----------------------|
  | curb (persistent)        | 2.519  | libcurl wrapper       |
  | em-http-request          | 2.731  | EM + http_parser.rb   |
  | Typhoeus                 | 2.851  | libcurl wrapper       |
  | StreamlyFFI (persistent) | 2.853  | libcurl wrapper       |
  | http.rb (persistent)     | 2.970  | Ruby + http_parser.rb |
  | http.rb                  | 3.588  | Ruby + http_parser.rb |
  | HTTParty                 | 3.931  | Net::HTTP wrapper     |
  | Net::HTTP                | 3.959  | Pure Ruby             |
  | Net::HTTP (persistent)   | 4.043  | Pure Ruby             |
  | open-uri                 | 4.479  | Net::HTTP wrapper     |
  | Excon (persistent)       | 4.618  | Pure Ruby             |
  | Excon                    | 4.701  | Pure Ruby             |
  | RestClient               | 26.838 | Net::HTTP wrapper     |
61 62 63

Benchmarks performed using excon's benchmarking tool

64 65 66 67
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!

68 69 70 71 72 73 74 75 76 77
## 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)
78 79 80

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

81
https://github.com/httprb/http/issues
82

83 84

## Installation
85 86

Add this line to your application's Gemfile:
87 88 89
```ruby
gem "http"
```
90 91

And then execute:
92 93 94
```bash
$ bundle
```
95 96

Or install it yourself as:
97 98 99
```bash
$ gem install http
```
100 101

Inside of your Ruby program do:
102 103 104
```ruby
require "http"
```
105 106 107

...to pull it in as a dependency.

108

109 110 111
## Documentation

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

114
The following API documentation is also available:
115

116 117
* [YARD API documentation](http://www.rubydoc.info/gems/http/frames)
* [Chainable module (all chainable methods)](http://www.rubydoc.info/gems/http/HTTP/Chainable)
118

119
### Basic Usage
120

121
Here's some simple examples to get you started:
122 123

```ruby
124
>> HTTP.get("https://github.com").to_s
125
=> "\n\n\n<!DOCTYPE html>\n<html lang=\"en\" class=\"\">\n  <head prefix=\"o..."
126 127
```

128
That's all it takes! To obtain an `HTTP::Response` object instead of the response
129
body, all we have to do is omit the `#to_s` on the end:
130 131

```ruby
132
>> HTTP.get("https://github.com")
133
=> #<HTTP::Response/1.1 200 OK {"Server"=>"GitHub.com", "Date"=>"Tue, 10 May...>
134 135
```

136
We can also obtain an `HTTP::Response::Body` object for this response:
137 138

```ruby
139
>> HTTP.get("https://github.com").body
140
=> #<HTTP::Response::Body:3ff756862b48 @streaming=false>
141 142
```

143 144 145
The response body can be streamed with `HTTP::Response::Body#readpartial`.
In practice, you'll want to bind the HTTP::Response::Body to a local variable
and call `#readpartial` on it repeatedly until it returns `nil`:
146 147

```ruby
148 149 150 151 152 153 154 155 156
>> body = HTTP.get("https://github.com").body
=> #<HTTP::Response::Body:3ff756862b48 @streaming=false>
>> body.readpartial
=> "\n\n\n<!DOCTYPE html>\n<html lang=\"en\" class=\"\">\n  <head prefix=\"o..."
>> body.readpartial
=> "\" href=\"/apple-touch-icon-72x72.png\">\n    <link rel=\"apple-touch-ic..."
# ...
>> body.readpartial
=> nil
157 158
```

159
## Supported Ruby Versions
160 161 162 163

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

164
* Ruby 2.2.x
165
* Ruby 2.3.x
166
* Ruby 2.4.x
167
* JRuby 9.1.x.x
168 169 170 171 172 173 174 175 176 177 178 179 180 181

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.

182
[travis]: http://travis-ci.org/httprb/http
183 184


185
## Contributing to http.rb
186

187 188 189 190
* Fork http.rb on GitHub
* Make your changes
* Ensure all tests pass (`bundle exec rake`)
* Send a pull request
191 192
* If we like them we'll merge them
* If we've accepted a patch, feel free to ask for commit access!
193 194


195 196
## Copyright

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