class HTTP::Client

Overview

An HTTP Client.

NOTE To use Client, you must explicitly import it with require "http/client"

One-shot usage

Without a block, an HTTP::Client::Response is returned and the response's body is available as a String by invoking HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.get "http://www.example.com"
response.status_code      # => 200
response.body.lines.first # => "<!doctype html>"

Parameters

Parameters can be added to any request with the URI::Params.encode method, which converts a Hash or NamedTuple to a URL encoded HTTP query.

require "http/client"

params = URI::Params.encode({"author" => "John Doe", "offset" => "20"}) # => "author=John+Doe&offset=20"
response = HTTP::Client.get URI.new("http", "www.example.com", query: params)
response.status_code # => 200

Streaming

With a block, an HTTP::Client::Response body is returned and the response's body is available as an IO by invoking HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.get("http://www.example.com") do |response|
  response.status_code  # => 200
  response.body_io.gets # => "<!doctype html>"
end

Reusing a connection

Similar to the above cases, but creating an instance of an HTTP::Client.

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.get "/"
response.status_code      # => 200
response.body.lines.first # => "<!doctype html>"
client.close

WARNING A single HTTP::Client instance is not safe for concurrent use by multiple fibers.

Compression

If compress isn't set to false, and no Accept-Encoding header is explicitly specified, an HTTP::Client will add an "Accept-Encoding": "gzip, deflate" header, and automatically decompress the response body/body_io.

Encoding

If a response has a Content-Type header with a charset, that charset is set as the encoding of the returned IO (or used for creating a String for the body). Invalid bytes in the given encoding are silently ignored when reading text content.

Defined in:

lib/http_proxy/src/ext/http/client.cr
chatgpt/utils/proxy.cr

Constructors

Constructor Detail

def self.new(uri : URI, tls : TLSContext = nil) #

Creates a new HTTP client from a URI. Parses the host, port, and tls configuration from the URI provided. Port defaults to 80 if not specified unless using the https protocol, which defaults to port 443 and sets tls to true.

require "http/client"
require "uri"

uri = URI.parse("https://secure.example.com")
client = HTTP::Client.new(uri)

client.tls? # => #<OpenSSL::SSL::Context::Client>
client.get("/")

This constructor will ignore any path or query segments in the URI as those will need to be passed to the client when a request is made.

If tls is given it will be used, if not a new TLS context will be created. If tls is given and uri is a HTTP URI, ArgumentError is raised. In any case the active context can be accessed through tls.

This constructor will raise an exception if any scheme but HTTP or HTTPS is used.


[View source]