HTTP::Request
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
Class methods
Returns a HTTP::Request instance if successfully parsed,
nil on EOF or HTTP::Status otherwise.
Instance methods
Returns a convenience wrapper around querying and setting cookie related
headers, see HTTP::Cookies.
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.
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.
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.
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.
Returns a convenience wrapper around querying and setting query params,
see URI::Params.
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.
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.
Returns the underlying URI object.
Used internally to provide the components of the request uri.