Making HTTP Requests in Ruby with the httpx Gem

Feb 5, 2024 ยท 2 min read

The httpx gem provides a simple and flexible way to make HTTP requests in Ruby. It wraps the standard library's Net::HTTP with a cleaner API and useful features like persistent connections and timeouts.

Getting Started

To install httpx:

gem install httpx

Then require it in your code:

require 'httpx'

Making a basic GET request is simple:

response = HTTPX.get("")

The response contains the status code, headers, and body:

puts response.code # 200
puts response.headers["Content-Type"] # "text/html"
puts response.body # "<html>...</html>"

Configuring Requests

You can customize requests in a variety of ways:

response = HTTPX.get("", 
  params: {key: "value"}, # query string parameters
  headers: {Authorization: "Bearer xyz"} # custom headers

Set timeouts, follow redirects, attach cookies, and more.

Handling Responses

httpx makes it easy to handle common status codes:

  response = HTTPX.get("") 
rescue HTTPX::NotFound
  puts "Whoops, resource not found!"

It raises custom errors for 400, 500, and other status codes.

Persistent Connections

Creating a client reuse TCP connections:

client =
client.get("") # reuses connection

This avoids overhead of creating new connections.

httpx wraps and simplifies Ruby's HTTP client with a clean interface, while providing useful functionality like persistent connections. It's great for APIs, web scraping, and tasks involving HTTP requests.

Browse by tags:

Browse by language:

The easiest way to do Web Scraping

Get HTML from any page with a simple API call. We handle proxy rotation, browser identities, automatic retries, CAPTCHAs, JavaScript rendering, etc automatically for you

Try ProxiesAPI for free

curl ""

<!doctype html>
    <title>Example Domain</title>
    <meta charset="utf-8" />
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />


Don't leave just yet!

Enter your email below to claim your free API key: