README.md 7.45 KB
Newer Older
1
![The HTTP Gem](https://raw.github.com/tarcieri/http/master/logo.png)
2 3 4 5 6 7
==============
[![Gem Version](https://badge.fury.io/rb/http.png)](http://rubygems.org/gems/http)
[![Build Status](https://secure.travis-ci.org/tarcieri/http.png?branch=master)](http://travis-ci.org/tarcieri/http)
[![Code Climate](https://codeclimate.com/github/tarcieri/http.png)](https://codeclimate.com/github/tarcieri/http)
[![Coverage Status](https://coveralls.io/repos/tarcieri/http/badge.png?branch=master)](https://coveralls.io/r/tarcieri/http)

8 9 10 11 12 13 14 15
SEO Note
--------

This Gem has the worst name in the history of SEO. But perhaps we can fix that if we
all refer to it as "The HTTP Gem", or even better, the "Ruby HTTP Gem".

About
-----
16 17

The HTTP Gem is an easy-to-use client library for making requests from Ruby. It uses
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
a simple method chaining system for building requests, similar to Python's [Requests]

Under the hood, The HTTP Gem uses [http_parser.rb], a fast HTTP parsing native
extension based on the Node.js parser and a Java port thereof.

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

Help and Discussion
-------------------

If you need help or just want to talk about the Ruby HTTP Gem, [visit our Google
Group][googlegroup], or join by email by sending a message to:
[ruby-http-gem+subscribe@googlegroups.com][subscribe].

[googlegroup]: https://groups.google.com/forum/#!forum/ruby-http-gem
[subscribe]:   mailto:ruby-http-gem+subscribe@googlegroups.com

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

https://github.com/tarcieri/http/issues
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

Installation
------------

Add this line to your application's Gemfile:

    gem 'http'

And then execute:

    $ bundle

Or install it yourself as:

    $ gem install http

Inside of your Ruby program do:

    require 'http'

...to pull it in as a dependency.

61 62 63 64 65
Documentation
-------------

[Please see the HTTP Gem Wiki](https://github.com/tarcieri/http/wiki)
for more detailed documentation and usage notes.
66

67 68 69 70 71 72
Basic Usage
-----------

Here's some simple examples to get you started:

### GET requests
73 74

```ruby
75
>> HTTP.get("http://www.google.com").to_s
76 77 78
=> "<html><head><meta http-equiv=\"content-type\" content=..."
```

79 80
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:
81 82

```ruby
83
>> HTTP.get("http://www.google.com")
84
=> #<HTTP/1.0 200 OK @headers={"Content-Type"=>"text/html; charset=UTF-8", "Date"=>"Fri, ...>
85
 => #<HTTP::Response/1.1 200 OK @headers={"Content-Type"=>"text/html; ...>
86 87
```

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
We can also obtain an `HTTP::ResponseBody` object for this response:

```ruby
>> HTTP.get("http://www.google.com").body
 => #<HTTP::ResponseBody:814d7aac @streaming=false>
```

The response body can be streamed with `HTTP::ResponseBody#readpartial`:

```ruby
>> HTTP.get("http://www.google.com").body.readpartial
 => "<!doctype html><html "
```

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

### POST requests

107 108 109 110 111 112 113 114 115 116 117 118 119 120
Making POST requests is simple too. Want to POST a form?

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

```ruby
HTTP.get "http://example.com/resource", :params => {:foo => "bar"}
```

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

```ruby
121 122 123 124 125 126 127 128 129 130 131 132 133 134
HTTP.post "http://example.com/resource", :json => { :foo => '42' })
```

It's easy!


### Proxy Support

Making request behind proxy is as simple as making them directly. Just specify
hostname (or IP address) of your proxy server and it's port, and here you go:

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

137
Proxy needs authentication? No problem:
138 139

```ruby
140 141
HTTP.via("proxy-hostname.local", 8080, "username", "password")
  .get "http://example.com/resource"
142 143 144
```


145
### Adding Headers
146

147
The HTTP gem uses the concept of chaining to simplify requests. Let's say
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
you want to get the latest commit of this library from Github in JSON format.
One way we could do this is by tacking a filename on the end of the URL:

```ruby
HTTP.get "https://github.com/tarcieri/http/commit/HEAD.json"
```

The Github API happens to support this approach, but really this is a bit of a
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
HTTP.with_headers(:accept => 'application/json').
  get("https://github.com/tarcieri/http/commit/HEAD")
```

This requests JSON from Github. Github is smart enough to understand our
request and returns a response with Content-Type: application/json. If you
happen to have a library loaded which defines the JSON constant and implements
169
JSON.parse, the HTTP gem will attempt to parse the JSON response.
170 171 172 173 174 175 176 177 178 179 180

Shorter aliases exists for HTTP.with_headers:

```ruby
HTTP.with(:accept => 'application/json').
  get("https://github.com/tarcieri/http/commit/HEAD")

HTTP[:accept => 'application/json'].
  get("https://github.com/tarcieri/http/commit/HEAD")
```

181
### Content Negotiation
182

183
As important a concept as content negotiation is to HTTP, it sure should be easy,
184 185 186 187 188 189 190 191 192 193 194
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
HTTP.accept(:json).get("https://github.com/tarcieri/http/commit/HEAD")
```

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


195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
### Celluloid::IO Support

The HTTP Gem makes it simple to make multiple concurrent HTTP requests from a
Celluloid::IO actor. Here's a parallel HTTP fetcher with the HTTP Gem and
Celluloid::IO:

```ruby
require 'celluloid/io'
require 'http'

class HttpFetcher
  include Celluloid::IO

  def fetch(url)
    HTTP.get(url, socket_class: Celluloid::IO::TCPSocket).response
  end
end
```

There's a little more to it, but that's the core idea!

* [Full parallel HTTP fetcher example](https://github.com/tarcieri/http/wiki/Parallel-requests-with-Celluloid%3A%3AIO)
* See also: [Celluloid::IO](https://github.com/celluloid/celluloid-io)


Supported Ruby Versions
-----------------------

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

* Ruby 1.8.7
* Ruby 1.9.2
* Ruby 1.9.3
* Ruby 2.0.0
* Ruby 2.1.0

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.

[travis]: http://travis-ci.org/tarcieri/http


Contributing to The HTTP Gem
----------------------------

* Fork the HTTP gem on github
252
* Make your changes and send me a pull request
253 254
* If we like them we'll merge them
* If we've accepted a patch, feel free to ask for commit access!
255 256 257 258

Copyright
---------

259
Copyright (c) 2014 Tony Arcieri, Erik Michaels-Ober. See LICENSE.txt for further details.