H

httpx

An HTTP client library for ruby

  • Tiago's avatar
    typo · c9f57704
    Tiago authored
    c9f57704
Name
Last commit
Last update
doc/release_notes typo
examples example script to test reconnection
lib bumped to 0.5.1
test added ipaddr explicitly for jruby
www bumped to 0.5.1
.dockerignore don't ignore doc
.gitignore Blog
.gitlab-ci.yml Blog
.rspec the .rspec
.rubocop-2.1.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.rubocop-2.2.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.rubocop-2.3.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.rubocop-2.4.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.rubocop-2.5.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.rubocop-2.6.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.rubocop.yml request exposes options: this allows responses to be created with the proper set of options, instead of the connection options, which is wrong
.rubocop_todo.yml added separate configs per ruby version, which helps keeping up the level of granularity way better than just having a single rubocop_todo.yml; fixed a few cases based on some missing cops
.simplecov Blog
CHANGELOG.md added changelog
CHEATSHEET.md some more examples in the cheatsheet
Gemfile Blog
LICENSE.txt added apache license
README.md Update README.md
Rakefile jekyll: reverted config.yml.erb, as it wasn't deploying properly
docker-compose-jruby-9.0.0.0.yml pool: allowing it to load more than one resolver per pool, depending on connection conditions; still reusing it whenever possible (right now, per type, which is still wrong, if we want to connect, let's say, to more than a different DNS server with the same type)
docker-compose-ruby-2.1.yml parametrized spec script, setting gitlab CI
docker-compose-ruby-2.2.yml parametrized spec script, setting gitlab CI
docker-compose-ruby-2.3.yml parametrized spec script, setting gitlab CI
docker-compose-ruby-2.4.yml parametrized spec script, setting gitlab CI
docker-compose-ruby-2.5.yml parametrized spec script, setting gitlab CI
docker-compose-ruby-2.6.yml added 2.6 to the CI suite
docker-compose.yml increased the number of workers both on nghttpx and httpbin, which decreased massively the number of sporadic timeouts; updated setups as well
httpx.gemspec added timers gem, and a timer group to the pool
spec.sh removed warning for --all flag

HTTPX: A Ruby HTTP library for tomorrow... and beyond!

pipeline status coverage report

HTTPX is an HTTP client library for the Ruby programming language.

Among its features, it supports:

  • HTTP/2 and HTTP/1.x protocol versions
  • Concurrent requests by default
  • Simple and chainable API (based on HTTP.rb, itself based on Python Requests)
  • Proxy Support (HTTP(S), Socks4/4a/5)
  • Simple Timeout System
  • Lightweight (explicit feature loading)

And among others

  • Compression (gzip, deflate, brotli)
  • Authentication (Basic Auth, Digest Auth)
  • Cookies
  • HTTP/2 Server Push
  • H2C Upgrade
  • Redirect following

How

Here are some simple examples:

HTTPX.get("https://nghttp2.org").to_s #=> "<!DOCT...."

And that's the simplest one there is.

If you want to do some more things with the response, you can get an HTTPX::Response:

response = HTTPX.get("https://nghttp2.org")
puts response.status #=> 200
body = response.body
puts body #=> #<HTTPX::Response ...

You can also send as many requests as you want simultaneously:

page1, page2, page3 = HTTPX.get("https://news.ycombinator.com/news", "https://news.ycombinator.com/news?p=2", "https://news.ycombinator.com/news?p=3")

Installation

Add this line to your Gemfile:

gem "httpx"

or install it in your system:

> gem install httpx

and then just require it in your program:

require "httpx"

Why Should I care?

In Ruby, HTTP client implementations are a known cheap commodity. Why this one?

Concurrency

This library supports HTTP/2 seamlessly (which means, if the request is secure, and the server support ALPN negotiation AND HTTP/2, the request will be made through HTTP/2). If you pass multiple URIs, and they can utilize the same connection, they will run concurrently in it.

However if the server supports HTTP/1.1, it will try to use HTTP pipelining, falling back to 1 request at a time if the server doesn't support it (if the server support Keep-Alive connections, it will reuse the same connection).

Clean API

HTTPX acknowledges the ease-of-use of the http gem API (itself inspired by python requests library). It therefore aims at reusing the same facade, extending it for the use cases which the http gem doesn't support.

Lightweight

It ships with a plugin system similar to the ones used by sequel, roda or shrine.

It means that it loads the bare minimum to perform requests, and the user has to explicitly load the plugins, in order to get the features he/she needs.

It also means that it ships with the minimum amount of dependencies.

DNS-over-HTTPS

HTTPX ships with custom DNS resolver implementations, including a DNS-over-HTTPS resolver.

Easy to test

The test suite runs against httpbin proxied over nghttp2, so there are no mocking/stubbing false positives. The test suite uses minitest, but its matchers usage is (almost) limited to #assert (assert is all you need).

Supported Rubies

All Rubies greater or equal to 2.1, and always latest JRuby.

Note: This gem is tested against all latest patch versions, i.e. if you're using 2.2.0 and you experience some issue, please test it against 2.2.10 (latest patch version of 2.2) before creating an issue.

Resources

Website https://honeyryderchuck.gitlab.io/httpx/
Documentation https://honeyryderchuck.gitlab.io/httpx/rdoc/
Wiki https://gitlab.com/honeyryderchuck/httpx/wikis/home
CI https://gitlab.com/honeyryderchuck/httpx/pipelines

Caveats

ALPN support

HTTPS TLS backend is ruby's own openssl gem.

If your requirement is to run requests over HTTP/2 and TLS, make sure you run a version of the gem which compiles OpenSSL 1.0.2 (Ruby 2.3 and higher are guaranteed to).

JRuby's openssl is based on Bouncy Castle, which is massively outdated and still doesn't implement ALPN. So HTTP/2 over TLS/ALPN negotiation is off until JRuby figures this out.

Known bugs

Doesn't work with ruby 2.4.0 for Windows (see #36).

Contributing

  • Discuss your contribution in an issue
  • Fork it
  • Make your changes, add some test
  • Ensure all tests pass (bundle exec rake test)
  • Open a Merge Request (that's Pull Request in Github-ish)
  • Wait for feedback