class Net::HTTP

Class Net::HTTP provides a rich library that implements the client in a client-server model that uses the HTTP request-response protocol. For information about HTTP, see:

About the Examples ¶ ↑

Examples here assume that net/http has been required (which also requires uri ):

require 'net/http' 

Many code examples here use these example websites:

Some examples also assume these variables:

uri = URI('https://jsonplaceholder.typicode.com/') uri.freeze # Examples may not modify. hostname = uri.hostname # => "jsonplaceholder.typicode.com" path = uri.path # => "/" port = uri.port # => 443 

So that example requests may be written as:

Net::HTTP.get(uri) Net::HTTP.get(hostname, '/index.html') Net::HTTP.start(hostname) do |http| http.get('/todos/1') http.get('/todos/2') end 

An example that needs a modified URI first duplicates uri , then modifies the duplicate:

_uri = uri.dup _uri.path = '/todos/1' 

Strategies ¶ ↑

# Return string response body. Net::HTTP.get(hostname, path) Net::HTTP.get(uri) # Write string response body to $stdout. Net::HTTP.get_print(hostname, path) Net::HTTP.get_print(uri) # Return response as Net::HTTPResponse object. Net::HTTP.get_response(hostname, path) Net::HTTP.get_response(uri) data = '' Net::HTTP.post(uri, data) params = title: 'foo', body: 'bar', userId: 1> Net::HTTP.post_form(uri, params) data = '' Net::HTTP.put(uri, data)
Net::HTTP.start(hostname) do |http| # Session started automatically before block execution. http.get(path) http.head(path) body = 'Some text' http.post(path, body) # Can also have a block. http.put(path, body) http.delete(path) http.options(path) http.trace(path) http.patch(path, body) # Can also have a block. http.copy(path) http.lock(path, body) http.mkcol(path, body) http.move(path) http.propfind(path, body) http.proppatch(path, body) http.unlock(path, body) # Session finished automatically at block exit. end 

The methods cited above are convenience methods that, via their few arguments, allow minimal control over the requests. For greater control, consider using request objects.

URIs ¶ ↑

On the internet, a URI (Universal Resource Identifier) is a string that identifies a particular resource. It consists of some or all of: scheme, hostname, path, query, and fragment; see URI syntax.

A Ruby URI::Generic object represents an internet URI . It provides, among others, methods scheme , hostname , path , query , and fragment .

Schemes ¶ ↑

An internet URI has a scheme.

The two schemes supported in Net::HTTP are 'https' and 'http' :

uri.scheme # => "https" URI('http://example.com').scheme # => "http" 

Hostnames ¶ ↑

A hostname identifies a server (host) to which requests may be sent:

hostname = uri.hostname # => "jsonplaceholder.typicode.com" Net::HTTP.start(hostname) do |http| # Some HTTP stuff. end 

Paths ¶ ↑

A host-specific path identifies a resource on the host:

_uri = uri.dup _uri.path = '/todos/1' hostname = _uri.hostname path = _uri.path Net::HTTP.get(hostname, path)

Queries ¶ ↑

A host-specific query adds name/value pairs to the URI:

_uri = uri.dup params = userId: 1, completed: false> _uri.query = URI.encode_www_form(params) _uri # => # Net::HTTP.get(_uri)

Fragments ¶ ↑

A URI fragment has no effect in Net::HTTP; the same data is returned, regardless of whether a fragment is included.

Request Headers ¶ ↑

Request headers may be used to pass additional information to the host, similar to arguments passed in a method call; each header is a name/value pair.

Each of the Net::HTTP methods that sends a request to the host has optional argument headers , where the headers are expressed as a hash of field-name/value pairs:

headers = Accept: 'application/json', Connection: 'Keep-Alive'> Net::HTTP.get(uri, headers)

See lists of both standard request fields and common request fields at Request Fields. A host may also accept other custom fields.

HTTP Sessions ¶ ↑

A session is a connection between a server (host) and a client that:

See example sessions at Strategies.

Session Using Net::HTTP.start ¶ ↑

If you have many requests to make to a single host (and port), consider using singleton method Net::HTTP.start with a block; the method handles the session automatically by:

In the block, you can use these instance methods, each of which that sends a single request: