A very in-depth comparison of Ruby HTTP clients in 2023. I try to avoid dependencies where possible, but the API of Ruby’s built-in HTTP client is so absolutely brutal that I’ve always defaulted to using Excon.
The author finds that HTTPX is the best choice, which you have to take with a grain of salt since the article’s author also wrote HTTPX (although the comparison is fair by my read).
HTTPX’s only dependency is http-2-next
, with no C extensions or other shenanigans. It’s feature rich, and will use powerful HTTP constructs by default. See this example where multiple requests are made concurrently automatically, and those targeting the same host will multiplex over the same socket with HTTP/2:
HTTPX.get(
"https://news.ycombinator.com/news",
"https://news.ycombinator.com/news?p=2",
"https://google.com/q=me"
) # first two requests will be multiplexed on the same socket.
The corner cases it can handle are genuinely impressive. For example, here’s how to target a request to an IP address over HTTPS and pass a specific hostname to verify using TLS SNI, something that almost no one will ever have to do, but great to have for anyone who finds themselves in that position:
HTTPX.get("https://172.45.65.131:5647/",
ssl: {hostname: "proxy-ssl"},
headers: {"host": "subapp.com:5647"})