- README

- Top Level Namespace
- Adler32
- ArgumentError
- Array
- Atomic
- Base64
  - Error
- Benchmark
  - BM
    - Job
    - Tms
  - IPS
    - Entry
    - Job
- BigFloat
- BigInt
- BigRational
- BitArray
- Bool
- Box
- Bytes
- Channel
  - Buffered
  - ClosedError
  - ReceiveAction
  - SelectAction
  - SendAction
  - Unbuffered
- Char
  - Reader
- Class
- Colorize
  - Object
  - ObjectExtensions
- Comparable
- Complex
- Concurrent
  - CanceledError
- CRC32
- Crypto
  - Bcrypt
    - Error
    - Password
  - Blowfish
  - Subtle
- Crystal
  - Macros
    - And
    - Arg
    - ArrayLiteral
    - Assign
    - ASTNode
    - BinaryOp
    - Block
    - BoolLiteral
    - Call
    - Case
    - Cast
    - CharLiteral
    - ClassDef
    - ClassVar
    - Def
    - Expressions
    - Generic
    - Global
    - HashLiteral
    - If
    - ImplicitObj
    - InstanceSizeOf
    - InstanceVar
    - IsA
    - Macro
    - MacroId
    - MetaVar
    - MultiAssign
    - NamedArgument
    - NamedTupleLiteral
    - NilableCast
    - NilLiteral
    - Nop
    - Not
    - NumberLiteral
    - Or
    - Out
    - Path
    - PointerOf
    - RangeLiteral
    - ReadInstanceVar
    - RegexLiteral
    - Require
    - RespondsTo
    - SizeOf
    - Splat
    - StringInterpolation
    - StringLiteral
    - SymbolLiteral
    - TupleLiteral
    - TypeDeclaration
    - TypeNode
    - UnaryExpression
    - UninitializedVar
    - Union
    - Var
    - VisibilityModifier
    - When
    - While
  - System
- CSV
  - Builder
    - Row
  - Error
  - Lexer
  - MalformedCSVError
  - Parser
  - Row
  - Token
    - Kind
- Debug
  - DWARF
    - Abbrev
      - Attribute
    - AT
    - FORM
    - Info
      - Value
    - LineNumbers
      - Register
      - Row
      - Sequence
    - LNE
    - LNS
    - Strings
    - TAG
  - ELF
    - Endianness
    - Error
    - Ident
    - Klass
    - Machine
    - OSABI
    - SectionHeader
      - Flags
      - Type
    - Type
- Deque
- Digest
  - Base
  - MD5
  - SHA1
- Dir
- DivisionByZero
- DL
- ECR
- Enum
- Enumerable
  - EmptyError
- ENV
- Errno
- Exception
- Fiber
- File
  - Stat
- FileUtils
- Flate
  - Error
  - Reader
  - Strategy
  - Writer
- Float
  - Primitive
  - Printer
    - CachedPowers
      - Power
    - DiyFP
    - Grisu3
    - IEEE
- Float32
- Float64
- GC
  - Stats
- Gzip
  - Error
  - Header
  - Reader
  - Writer
- Hash
- HTML
- HTTP
  - Client
    - BodyType
    - Response
  - CompressHandler
  - ComputedContentTypeHeader
  - Cookie
  - Cookies
  - ErrorHandler
  - FormData
    - Builder
    - Error
    - FileMetadata
    - Parser
    - Part
  - Handler
    - Proc
  - Headers
  - LogHandler
  - Multipart
    - Builder
    - Error
    - Parser
  - Params
    - Builder
  - Request
  - Server
    - Context
    - RequestProcessor
    - Response
  - StaticFileHandler
    - DirectoryListing
  - WebSocket
  - WebSocketHandler
- Indexable
- IndexError
- INI
- Int
  - Primitive
  - Signed
  - Unsigned
- Int16
- Int32
- Int64
- Int8
- InvalidByteSequenceError
- IO
  - Buffered
  - ByteFormat
    - BigEndian
    - LittleEndian
    - NetworkEndian
    - SystemEndian
  - Delimited
  - EncodingOptions
  - EOFError
  - Error
  - FileDescriptor
  - Hexdump
  - Memory
  - MultiWriter
  - Seek
  - Sized
  - Timeout
- IPSocket
- Iterable
- Iterator
  - IteratorWrapper
  - Stop
- JSON
  - Any
  - Builder
    - ArrayState
    - DocumentEndState
    - DocumentStartState
    - ObjectState
    - StartState
    - State
  - Error
  - Lexer
  - ParseException
  - Parser
  - PullParser
  - Token
  - Type
- KeyError
- Levenshtein
  - Finder
- LLVM
  - ABI
    - AArch64
    - ArgKind
    - ArgType
    - ARM
    - FunctionType
    - X86
    - X86_64
      - RegClass
  - AtomicOrdering
  - AtomicRMWBinOp
  - Attribute
  - AttributeIndex
  - BasicBlock
  - BasicBlockCollection
  - Builder
  - CallConvention
  - CodeGenFileType
  - CodeGenOptLevel
  - CodeModel
  - Context
  - DIBuilder
  - DIFlags
  - DwarfTag
  - DwarfTypeEncoding
  - Function
  - FunctionCollection
  - FunctionPassManager
    - Runner
  - GenericValue
  - GlobalCollection
  - InstructionCollection
  - IntPredicate
  - JITCompiler
  - Linkage
  - MemoryBuffer
  - Module
  - ModuleFlag
  - ModulePassManager
  - OperandBundleDef
  - ParameterCollection
  - PassManagerBuilder
  - PassRegistry
  - PhiTable
  - RealPredicate
  - RelocMode
  - Target
  - TargetData
  - TargetMachine
  - Type
    - Kind
  - Value
    - Kind
  - ValueMethods
  - VerifierFailureAction
- Logger
  - Formatter
  - Severity
- Markdown
  - HTMLRenderer
  - Parser
    - PrefixHeader
    - UnorderedList
  - Renderer
- Math
- Mutex
- NamedTuple
- Nil
- Number
  - Primitive
- OAuth
  - AccessToken
  - Consumer
  - Error
  - RequestToken
- OAuth2
  - AccessToken
    - Bearer
    - Mac
  - Client
  - Error
  - Session
- Object
- OpenSSL
  - Digest
    - Error
    - UnsupportedError
  - DigestBase
  - DigestIO
    - DigestMode
  - Error
  - HMAC
  - MD5
  - SHA1
  - SSL
    - Context
      - Client
      - Server
    - Error
    - ErrorType
    - Modes
    - Options
    - Socket
      - Client
      - Server
    - VerifyMode
    - X509VerifyFlags
- OptionParser
  - Exception
  - InvalidOption
  - MissingOption
- PartialComparable
- Pointer
  - Appender
- PrettyPrint
- Proc
- Process
  - Env
  - Status
  - Stdio
  - Tms
- Random
  - ISAAC
  - MT19937
  - PCG32
  - System
- Range
- Readline
  - CompletionProc
  - KeyBindingProc
- Reference
- Reflect
- Regex
  - MatchData
  - Options
- SecureRandom
- Set
- Signal
- Slice
- Socket
  - Address
  - Addrinfo
  - Error
  - Family
  - IPAddress
  - Protocol
  - Server
  - Type
  - UNIXAddress
- Spec
  - Expectations
  - Methods
  - ObjectExtensions
- StaticArray
- String
  - Builder
  - RawConverter
- StringPool
- StringScanner
- Struct
- Symbol
- System
- TCPServer
- TCPSocket
- Tempfile
- Termios
  - AttributeSelection
  - BaudRate
  - ControlMode
  - InputMode
  - LineControl
  - LocalMode
  - OutputMode
- Time
  - DayOfWeek
  - EpochConverter
  - EpochMillisConverter
  - Format
    - Error
  - Kind
  - MonthSpan
  - Span
- Tuple
- TypeCastError
- UDPSocket
- UInt16
- UInt32
- UInt64
- UInt8
- Unicode
  - CaseOptions
- Union
- UNIXServer
- UNIXSocket
- URI
  - Error
- Value
- WeakRef
- XML
  - Attributes
  - AttributeType
  - Builder
  - Error
  - HTMLParserOptions
  - Namespace
  - Node
  - NodeSet
  - ParserOptions
  - Reader
  - SaveOptions
  - Type
  - XPathContext
- YAML
  - Any
  - Builder
  - Error
  - EventKind
  - ParseException
  - Parser
  - PullParser
  - Type
- Zip
  - CompressionMethod
  - Error
  - File
    - Entry
  - FileInfo
  - Reader
    - Entry
  - Writer
    - Entry
- Zlib
  - Error
  - Reader
  - Writer

# class HTTP::Client

- HTTP::Client
- Reference
- Object

## 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>"

### 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:

## Constructors

- .new(uri : URI, tls = nil, &block)

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

- .new(host : String, port = nil, tls = false, &block)

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

- .new(uri : URI, tls = nil)

  Creates a new HTTP client from a URI.

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

## Class Method Summary

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

  Executes a DELETE request and yields the response to the block.

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

  Executes a DELETE request.

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

  Executes a request.

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

  Executes a request.

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

  Executes a GET request.

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

  Executes a GET request and yields the response to the block.

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

  Executes a HEAD request and yields the response to the block.

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

  Executes a HEAD request.

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

  Executes a PATCH request.

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

  Executes a PATCH request and yields the response to the block.

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

  Executes a POST request and yields the response to the block.

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

  Executes a POST request.

- .post_form(url, form : String | IO | Hash, headers : HTTP::Headers? = nil, tls = nil) : HTTP::Client::Response

  Executes a POST with form data.

- .post_form(url, form : String | IO | Hash, headers : HTTP::Headers? = nil, tls = nil, &block)

  Executes a POST with form data and yields the response to the block.

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

  Executes a PUT request.

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

  Executes a PUT request and yields the response to the block.

## Instance Method Summary

- #basic_auth(username, password)

  Configures this client to perform basic authentication in every request.

- #before_request(&callback : HTTP::Request -> )

  Adds a callback to execute before each request.

- #close

  Closes this client.

- #compress=(compress : Bool)

  Whether automatic compression/decompression is enabled.

- #compress? : Bool

  Whether automatic compression/decompression is enabled.

- #connect_timeout=(connect_timeout : Time::Span)

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

- #connect_timeout=(connect_timeout : Number)

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

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

  Executes a DELETE request and yields the response to the block.

- #delete(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a DELETE request.

- #dns_timeout=(dns_timeout : Number)

  This method has no effect right now

- #dns_timeout=(dns_timeout : Time::Span)

  This method has no effect right now

- #exec(method : String, path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a request.

- #exec(request : HTTP::Request) : HTTP::Client::Response

  Executes a request.

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

  Executes a request.

- #exec(request : HTTP::Request, &block)

  Executes a request request and yields an `HTTP::Client::Response` to the block.

- #get(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a GET request.

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

  Executes a GET request and yields the response to the block.

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

  Executes a HEAD request and yields the response to the block.

- #head(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a HEAD request.

- #host : String

  Returns the target host.

- #patch(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a PATCH request.

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

  Executes a PATCH request and yields the response to the block.

- #port : Int32

  Returns the target port.

- #post(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a POST request.

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

  Executes a POST request and yields the response to the block.

- #post_form(path, form : String | IO, headers : HTTP::Headers? = nil) : HTTP::Client::Response

  Executes a POST with form data.

- #post_form(path, form : String | IO, headers : HTTP::Headers? = nil, &block)

  Executes a POST with form data and yields the response to the block.

- #post_form(path, form : Hash(String, _) | NamedTuple, headers : HTTP::Headers? = nil) : HTTP::Client::Response

  Executes a POST with form data.

- #post_form(path, form : Hash(String, _) | NamedTuple, headers : HTTP::Headers? = nil, &block)

  Executes a POST with form data and yields the response to the block.

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

  Executes a PUT request and yields the response to the block.

- #put(path, headers : HTTP::Headers? = nil, body : BodyType = nil) : HTTP::Client::Response

  Executes a PUT request.

- #read_timeout=(read_timeout : Time::Span)

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

- #read_timeout=(read_timeout : Number)

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

- #tls
- #tls? : OpenSSL::SSL::Context::Client?

### Instance methods inherited from class `Reference`

==(other : self)
==(other) ==, dup dup, hash hash, inspect(io : IO) : Nil inspect, object_id : UInt64 object_id, pretty_print(pp) : Nil pretty_print, same?(other : Reference)
same?(other : Nil) same?, to_s(io : IO) : Nil to_s

### Constructor methods inherited from class `Reference`

new new

### Instance methods inherited from class `Object`

!=(other) !=, !~(other) !~, ==(other) ==, ===(other : JSON::Any)
===(other : YAML::Any)
===(other) ===, =~(other) =~, class class, dup dup, hash hash, inspect(io : IO)
inspect inspect, itself itself, not_nil! not_nil!, pretty_inspect(width = 79, newline = "\n", indent = 0) : String pretty_inspect, pretty_print(pp : PrettyPrint) : Nil pretty_print, tap(&block) tap, to_json(io : IO)
to_json to_json, to_pretty_json(indent : String = " ")
to_pretty_json(io : IO, indent : String = " ") to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try, unsafe_as(type : T.class) forall T unsafe_as

### Constructor methods inherited from class `Object`

from_json(string_or_io, root : String) : self
from_json(string_or_io) : self from_json, from_yaml(string_or_io) : self from_yaml

## Constructor Detail

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

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`.

    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) #

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

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

def self.new(uri : URI, tls = 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`.

    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(host : String, port = nil, tls : Bool | OpenSSL::SSL::Context::Client = false) #

## Class Method Detail

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

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`.

    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::Response #

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

    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) #

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

    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::Response #

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

    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::Response #

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

    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) #

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`.

    HTTP::Client.get("/", 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, &block) #

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`.

    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::Response #

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

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

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

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

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

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

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`.

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

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

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`.

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

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

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

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

def self.post_form(url, form : String | IO | Hash, headers : HTTP::Headers? = nil, tls = nil) : HTTP::Client::Response #

Executes a POST with form data. The "Content-type" header is set to "application/x-www-form-urlencoded".

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

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

Executes a POST 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".

    HTTP::Client.post_form("http://www.example.com", "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::Response #

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

    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) #

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`.

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

## Instance Method Detail

def basic_auth(username, password) #

Configures this client to perform basic authentication in every request.

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

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

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

def close #

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

def compress=(compress : Bool) #

Whether automatic compression/decompression is enabled.

def compress? : Bool #

Whether automatic compression/decompression is enabled.

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

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

    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) #

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

    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, body : BodyType = nil, &block) #

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`.

    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, body : BodyType = nil) : HTTP::Client::Response #

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

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

def dns_timeout=(dns_timeout : Number) #

This method has no effect right now

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

    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) #

This method has no effect right now

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

    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::Response #

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

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

def exec(request : HTTP::Request) : HTTP::Client::Response #

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

    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) #

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

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

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

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`.

    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, body : BodyType = nil) : HTTP::Client::Response #

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

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

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

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`.

    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 head(path, headers : HTTP::Headers? = nil, body : BodyType = nil, &block) #

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`.

    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::Response #

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

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

def host : String #

Returns the target host.

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

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

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

    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, body : BodyType = nil, &block) #

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`.

    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 port : Int32 #

Returns the target port.

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

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

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

    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, body : BodyType = nil, &block) #

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`.

    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 post_form(path, form : String | IO, headers : HTTP::Headers? = nil) : HTTP::Client::Response #

Executes a POST with form data. The "Content-type" header is set to "application/x-www-form-urlencoded".

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

def post_form(path, form : String | IO, headers : HTTP::Headers? = nil, &block) #

Executes a POST 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".

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

def post_form(path, form : Hash(String, _) | NamedTuple, headers : HTTP::Headers? = nil) : HTTP::Client::Response #

Executes a POST with form data. The "Content-type" header is set to "application/x-www-form-urlencoded".

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

def post_form(path, form : Hash(String, _) | NamedTuple, headers : HTTP::Headers? = nil, &block) #

Executes a POST 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".

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

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

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`.

    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, body : BodyType = nil) : HTTP::Client::Response #

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

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

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

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

    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) #

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

    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? #