Net::HTTP

Added in v1.8 · Updated March 13, 2026 · Modules
ruby stdlib net http web

Net::HTTP is Ruby’s standard library for making HTTP requests. It supports all common HTTP methods (GET, POST, PUT, DELETE, etc.), handles response bodies, manages connections, and works with HTTPS through TLS.

Loading the Library

require 'net/http'
require 'uri'
require 'json' # for parsing API responses

Basic Usage

GET Request

The simplest way to make a GET request:

uri = URI('https://api.example.com/data')
response = Net::HTTP.get_response(uri)

response.body     # => "{\"status\":\"ok\"}"
response.code     # => "200"
response.message  # => "OK"

POST Request

uri = URI('https://api.example.com/create')
request = Net::HTTP::Post.new(uri)
request.body = JSON.dump({ name: 'Alice', email: 'alice@example.com' })
request['Content-Type'] = 'application/json'

response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
  http.request(request)
end

response.code # => "201"

Constructor Parameters

ParameterTypeDefaultDescription
addressStringrequiredHostname or IP address
portInteger80 (HTTP), 443 (HTTPS)Port number
proxy_addrStringnilProxy server address
proxy_portIntegernilProxy server port
open_timeoutIntegernilConnection timeout (seconds)
read_timeoutIntegernilRead timeout (seconds)
use_sslBooleanfalseEnable HTTPS

Common Methods

Net::HTTP.get_response(uri)

Synchronous GET request. Returns a Net::HTTPResponse object.

response = Net::HTTP.get_response(URI('https://example.com'))

Net::HTTP.post(uri, body, headers)

Synchronous POST request.

response = Net::HTTP.post(
  URI('https://api.example.com/login'),
  'username=admin&password=secret',
  { 'Content-Type' => 'application/x-www-form-urlencoded' }
)

Net::HTTP.start(host, port, use_ssl: false) { |http| ... }

Opens a connection and yields the HTTP object. The connection closes automatically when the block finishes.

Net::HTTP.start('api.example.com', 443, use_ssl: true) do |http|
  request = Net::HTTP::Get.new('/users')
  response = http.request(request)
  JSON.parse(response.body)
end

Net::HTTP.new(address, port)

Creates a new HTTP object without connecting. Use start to open the connection.

http = Net::HTTP.new('api.example.com', 443)
http.use_ssl = true
http.start

response = http.get('/data')
http.finish

Request Types

# All HTTP methods are supported
Net::HTTP::Get.new(uri)
Net::HTTP::Post.new(uri)
Net::HTTP::Put.new(uri)
Net::HTTP::Delete.new(uri)
Net::HTTP::Patch.new(uri)
Net::HTTP::Head.new(uri)
Net::HTTP::Options.new(uri)

Handling Responses

response = Net::HTTP.get_response(URI('https://api.example.com/data'))

# Check status code
if response.code == '200'
  data = JSON.parse(response.body)
end

# Work with headers
response['Content-Type']    # => "application/json"
response['X-Request-Id']    # => "abc123"

# Iterate over all headers
response.each_header do |name, value|
  puts "#{name}: #{value}"
end

Working with JSON APIs

require 'net/http'
require 'uri'
require 'json'

def fetch_json(url)
  uri = URI(url)
  response = Net::HTTP.get_response(uri)

  case response
  when Net::HTTPSuccess
    JSON.parse(response.body, symbolize_names: true)
  else
    raise "HTTP Error: #{response.code} #{response.message}"
  end
end

users = fetch_json('https://api.example.com/users')

See Also

  • json — parse and generate JSON data (commonly used with Net::HTTP)
  • yaml — alternative serialization format for configuration and data
  • hash-fetch — safely access hash values when working with response data