class

HTTP::Request

Inherits Reference / Object

An HTTP request.

It serves both to perform requests by an HTTP::Client and to represent requests received by an HTTP::Server.

A request always holds an IO as a body. When creating a request with a String or Bytes its body will be a IO::Memory wrapping these, and the Content-Length header will be set appropriately.

NOTE: To use Request, you must explicitly import it with require "http/request"

Constructors

new(method : String, resource : String, headers : Headers | Nil = nil, body : String | Bytes | IO | Nil = nil, version : String = "HTTP/1.1") : self
Source

Class methods

from_io(io : IO, *, max_request_line_size : Int32 = HTTP::MAX_REQUEST_LINE_SIZE, max_headers_size : Int32 = HTTP::MAX_HEADERS_SIZE) : HTTP::Request | HTTP::Status | Nil

Returns a HTTP::Request instance if successfully parsed, nil on EOF or HTTP::Status otherwise.

Source

Instance methods

body
Source
body=(body : String) : String
Source
body=(body : Bytes) : String
Source
body=(body : IO) : IO
Source
body=(body : Nil) : Nil
Source
content_length
Source
content_length=(length : Int) : String
Source
cookies

Returns a convenience wrapper around querying and setting cookie related headers, see HTTP::Cookies.

Source
form_params

Returns a convenience wrapper to parse form params, see URI::Params.

Source
form_params?

Returns a convenience wrapper to parse form params, see URI::Params. Returns nil in case the content type "application/x-www-form-urlencoded" is not present or the body is nil.

Source
headers
Source
headers=(headers : Headers)
Source
host_with_port

Returns request host with port from headers.

Source
hostname

Extracts the hostname from Host header.

Returns nil if the Host header is missing.

If the Host header contains a port number, it is stripped off.

Source
if_match
Source
if_none_match
Source
ignore_body?
Source
keep_alive?
Source
local_address

The network address of the HTTP server.

HTTP::Server will try to fill this property, and its value will have a format like "IP:port", but this format is not guaranteed. Middlewares can overwrite this value.

This property is not used by HTTP::Client.

Source
local_address=(local_address : Socket::Address | Nil)

The network address of the HTTP server.

HTTP::Server will try to fill this property, and its value will have a format like "IP:port", but this format is not guaranteed. Middlewares can overwrite this value.

This property is not used by HTTP::Client.

Source
method
Source
method=(method : String)
Source
path

Returns the request's path component.

Source
path=(path : String) : String

Sets request's path component.

Source
query

Lazily parses and returns the request's query component.

Source
query=(value : String | Nil) : String | Nil

Sets request's query component.

Source
query_params

Returns a convenience wrapper around querying and setting query params, see URI::Params.

Source
remote_address

The network address that sent the request to an HTTP server.

HTTP::Server will try to fill this property, and its value will have a format like "IP:port", but this format is not guaranteed. Middlewares can overwrite this value.

Example:

class ForwarderHandler
  include HTTP::Handler

  def call(context)
    if ip = context.request.headers["X-Real-IP"]? # When using a reverse proxy that guarantees this field.
      context.request.remote_address = Socket::IPAddress.new(ip, 0)
    end
    call_next(context)
  end
end

server = HTTP::Server.new([ForwarderHandler.new, HTTP::LogHandler.new])

This property is not used by HTTP::Client.

Source
remote_address=(remote_address : Socket::Address | Nil)

The network address that sent the request to an HTTP server.

HTTP::Server will try to fill this property, and its value will have a format like "IP:port", but this format is not guaranteed. Middlewares can overwrite this value.

Example:

class ForwarderHandler
  include HTTP::Handler

  def call(context)
    if ip = context.request.headers["X-Real-IP"]? # When using a reverse proxy that guarantees this field.
      context.request.remote_address = Socket::IPAddress.new(ip, 0)
    end
    call_next(context)
  end
end

server = HTTP::Server.new([ForwarderHandler.new, HTTP::LogHandler.new])

This property is not used by HTTP::Client.

Source
resource
Source
to_io(io : IO) : Nil
Source
uri

Returns the underlying URI object.

Used internally to provide the components of the request uri.

Source
version
Source
version=(version : String)
Source