W3cubDocs

/Crystal 0.31

class HTTP::Client

Overview

An 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 HTTP::Params#encode method, which converts a Hash or NamedTuple to a URL encoded HTTP query.

require "http/client"

params = HTTP::Params.encode({"author" => "John Doe", "offset" => "20"}) # => author=John+Doe&offset=20
response = HTTP::Client.get "http://www.example.com?" + 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

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:

http/client.cr

Constructors

Class Method Summary

Instance Method Summary

Constructor Detail

def self.new(host : String, port = nil, tls : Bool | OpenSSL::SSL::Context::Client = false)Source

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

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.

def self.new(uri : URI, tls = nil, &block)Source

Creates a new HTTP client from a URI, yields it to the block and closes the client afterwards. 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")
HTTP::Client.new(uri) do |client|
  client.tls? # => #<OpenSSL::SSL::Context::Client>
  client.get("/")
end

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.

def self.new(host : String, port = nil, tls = false, &block)Source

Creates a new HTTP client, yields it to the block, and closes the client afterwards.

require "http/client"

HTTP::Client.new("www.example.com") do |client|
  client.get "/"
end

Class Method Detail

def self.delete(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a DELETE request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.delete("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.delete(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a DELETE request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.delete "http://www.example.com", form: "foo=bar"

def self.delete(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a DELETE request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.delete("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.delete(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a DELETE request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.delete("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.exec(method, url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a request. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.exec("GET", "http://www.example.com") do |response|
  response.body_io.gets # => "..."
end

def self.exec(method, url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a request. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

response = HTTP::Client.exec "GET", "http://www.example.com"
response.body # => "..."

def self.get(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a GET request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.get("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.get(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a GET request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.get("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.get(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a GET request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.get "http://www.example.com", form: "foo=bar"

def self.get(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a GET request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.get("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.head(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a HEAD request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.head("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.head(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a HEAD request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.head "http://www.example.com", form: "foo=bar"

def self.head(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a HEAD request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.head("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.head(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a HEAD request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.head("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.options(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a OPTIONS request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.options("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.options(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a OPTIONS request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.options "http://www.example.com", form: "foo=bar"

def self.options(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a OPTIONS request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.options("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.options(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a OPTIONS request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.options("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.patch(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a PATCH request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.patch("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.patch(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a PATCH request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.patch "http://www.example.com", form: "foo=bar"

def self.patch(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a PATCH request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.patch("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.patch(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a PATCH request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.patch("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.post(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a POST request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.post("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.post(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a POST request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.post("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.post(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a POST request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.post "http://www.example.com", form: "foo=bar"

def self.post(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a POST request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.post("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.put(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil) : HTTP::Client::ResponseSource

Executes a PUT request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

response = HTTP::Client.put("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def self.put(url : String | URI, headers : HTTP::Headers? = nil, body : BodyType = nil, tls = nil, &block)Source

Executes a PUT request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

HTTP::Client.put("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def self.put(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash, &block)Source

Executes a PUT request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

HTTP::Client.put("http://www.example.com", form: "foo=bar") do |response|
  response.body_io.gets
end

def self.put(url, headers : HTTP::Headers? = nil, tls = nil, *, form : String | IO | Hash) : HTTP::Client::ResponseSource

Executes a PUT request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

response = HTTP::Client.put "http://www.example.com", form: "foo=bar"

Instance Method Detail

def basic_auth(username, password)Source

Configures this client to perform basic authentication in every request.

def before_request(&callback : HTTP::Request -> )Source

Adds a callback to execute before each request. This is usually used to set an authorization header. Any number of callbacks can be added.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.before_request do |request|
  request.headers["Authorization"] = "XYZ123"
end
client.get "/"

def closeSource

Closes this client. If used again, a new connection will be opened.

def compress=(compress : Bool)Source

Whether automatic compression/decompression is enabled.

def compress? : BoolSource

Whether automatic compression/decompression is enabled.

def connect_timeout=(connect_timeout : Time::Span)Source

Sets the open timeout with a Time::Span to wait when connecting, before raising an IO::Timeout.

require "http/client"

client = HTTP::Client.new("example.org")
client.connect_timeout = 5.minutes
begin
  response = client.get("/")
rescue IO::Timeout
  puts "Timeout!"
end

def connect_timeout=(connect_timeout : Number)Source

Sets the number of seconds to wait when connecting, before raising an IO::Timeout.

require "http/client"

client = HTTP::Client.new("example.org")
client.connect_timeout = 1.5
begin
  response = client.get("/")
rescue IO::Timeout
  puts "Timeout!"
end

def delete(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a DELETE request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.delete("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def delete(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a DELETE request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.delete("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def delete(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a DELETE request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.delete("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def delete(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a DELETE request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.delete "/", form: "foo=bar"

def delete(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a DELETE request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.delete("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def delete(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a DELETE request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.delete "/", form: {"foo" => "bar"}

def dns_timeout=(dns_timeout : Number)Source

This method has no effect right now

Sets the number of seconds to wait when resolving a name, before raising an IO::Timeout.

require "http/client"

client = HTTP::Client.new("example.org")
client.dns_timeout = 1.5
begin
  response = client.get("/")
rescue IO::Timeout
  puts "Timeout!"
end

def dns_timeout=(dns_timeout : Time::Span)Source

This method has no effect right now

Sets the number of seconds to wait when resolving a name with a Time::Span, before raising an IO::Timeout.

require "http/client"

client = HTTP::Client.new("example.org")
client.dns_timeout = 1.5.seconds
begin
  response = client.get("/")
rescue IO::Timeout
  puts "Timeout!"
end

def exec(method : String, path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.exec "GET", "/"
response.body # => "..."

def exec(request : HTTP::Request) : HTTP::Client::ResponseSource

Executes a request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.exec HTTP::Request.new("GET", "/")
response.body # => "..."

def exec(method : String, path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a request. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new "www.example.com"
client.exec("GET", "/") do |response|
  response.body_io.gets # => "..."
end

def exec(request : HTTP::Request, &block)Source

Executes a request request and yields an HTTP::Client::Response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new "www.example.com"
client.exec(HTTP::Request.new("GET", "/")) do |response|
  response.body_io.gets # => "..."
end

def get(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a GET request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.get("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def get(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a GET request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.get "/", form: {"foo" => "bar"}

def get(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a GET request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.get("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def get(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a GET request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.get "/", form: "foo=bar"

def get(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a GET request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.get("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def get(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a GET request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.get("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def head(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a HEAD request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.head("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def head(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a HEAD request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.head "/", form: {"foo" => "bar"}

def head(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a HEAD request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.head("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def head(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a HEAD request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.head "/", form: "foo=bar"

def head(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a HEAD request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.head("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def head(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a HEAD request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.head("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def host : StringSource

Returns the target host.

require "http/client"

client = HTTP::Client.new "www.example.com"
client.host # => "www.example.com"

def options(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a OPTIONS request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.options("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def options(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a OPTIONS request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.options("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def options(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a OPTIONS request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.options "/", form: {"foo" => "bar"}

def options(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a OPTIONS request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.options("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def options(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a OPTIONS request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.options("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def options(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a OPTIONS request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.options "/", form: "foo=bar"

def patch(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a PATCH request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.patch("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def patch(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a PATCH request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.patch "/", form: {"foo" => "bar"}

def patch(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a PATCH request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.patch("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def patch(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a PATCH request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.patch("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def patch(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a PATCH request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.patch("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def patch(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a PATCH request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.patch "/", form: "foo=bar"

def port : Int32Source

Returns the target port.

require "http/client"

client = HTTP::Client.new "www.example.com"
client.port # => 80

def post(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a POST request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.post("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def post(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a POST request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.post "/", form: "foo=bar"

def post(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a POST request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.post("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def post(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a POST request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.post "/", form: {"foo" => "bar"}

def post(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a POST request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.post("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def post(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a POST request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.post("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def put(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple, &block)Source

Executes a PUT request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.put("/", form: {"foo" => "bar"}) do |response|
  response.body_io.gets
end

def put(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::ResponseSource

Executes a PUT request. The response will have its body as a String, accessed via HTTP::Client::Response#body.

require "http/client"

client = HTTP::Client.new("www.example.com")
response = client.put("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!")
response.body #=> "..."

def put(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block)Source

Executes a PUT request and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io.

require "http/client"

client = HTTP::Client.new("www.example.com")
client.put("/", headers: HTTP::Headers{"User-Agent" => "AwesomeApp"}, body: "Hello!") do |response|
  response.body_io.gets #=> "..."
end

def put(path, headers : HTTP::Headers? = nil, *, form : String | IO) : HTTP::Client::ResponseSource

Executes a PUT request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.put "/", form: "foo=bar"

def put(path, headers : HTTP::Headers? = nil, *, form : String | IO, &block)Source

Executes a PUT request with form data and yields the response to the block. The response will have its body as an IO accessed via HTTP::Client::Response#body_io. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
client.put("/", form: "foo=bar") do |response|
  response.body_io.gets
end

def put(path, headers : HTTP::Headers? = nil, *, form : Hash(String, String) | NamedTuple) : HTTP::Client::ResponseSource

Executes a PUT request with form data and returns a Response. The "Content-Type" header is set to "application/x-www-form-urlencoded".

require "http/client"

client = HTTP::Client.new "www.example.com"
response = client.put "/", form: {"foo" => "bar"}

def read_timeout=(read_timeout : Time::Span)Source

Sets the read timeout with a Time::Span, to wait when reading before raising an IO::Timeout.

require "http/client"

client = HTTP::Client.new("example.org")
client.read_timeout = 5.minutes
begin
  response = client.get("/")
rescue IO::Timeout
  puts "Timeout!"
end

def read_timeout=(read_timeout : Number)Source

Sets the number of seconds to wait when reading before raising an IO::Timeout.

require "http/client"

client = HTTP::Client.new("example.org")
client.read_timeout = 1.5
begin
  response = client.get("/")
rescue IO::Timeout
  puts "Timeout!"
end

def tls

def tls? : OpenSSL::SSL::Context::Client?

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/HTTP/Client.html