class

HTTP::Server::Response

Inherits IO / Reference / Object

The response to configure and write to in an HTTP::Server handler.

The response status and headers must be configured before writing the response body. Once response output is written, changing the status and headers properties has no effect.

The HTTP::Server::Response is a write-only IO, so all IO methods are available in it.

A response can be upgraded with the upgrade method. Once invoked, headers are written and the connection IO (a socket) is yielded to the given block. This is useful to implement protocol upgrades, such as websockets.

Instance methods

close

Closes this response, writing headers and body if not done yet. This method must be implemented if wrapping the response output.

Source
closed?

Returns true if this response has been closed.

Source
content_length

Convenience method to get the Content-Length header.

Source
content_length=(content_length : Int) : String

Convenience method to set the Content-Length header.

Source
content_type

Convenience method to get Content-Type header.

Source
content_type=(content_type : String) : String

Convenience method to set the Content-Type header.

Source
cookies

Convenience method to set cookies, see HTTP::Cookies.

Source
flush

Flushes the output. This method must be implemented if wrapping the response output.

Source
headers

The response headers (HTTP::Headers). These must be set before writing to the response.

Source
output

The IO to which output is written. This can be changed/wrapped to filter the response body (for example to compress the output).

Source
output=(output : IO)

The IO to which output is written. This can be changed/wrapped to filter the response body (for example to compress the output).

Source
redirect(location : String | URI, status : HTTP::Status = :found) : Nil

Sends a redirect to location.

The value of location gets encoded with URI.encode.

The status determines the HTTP status code which can be HTTP::Status::FOUND (302) for a temporary redirect or HTTP::Status::MOVED_PERMANENTLY (301) for a permanent redirect.

The response gets closed.

Raises IO::Error if the response is closed or headers were already sent.

Source
respond_with_status(status : HTTP::Status, message : String | Nil = nil) : Nil

Sends status and message as response.

This method calls #reset to remove any previous settings and writes the given status and message to the response IO. Finally, it closes the response.

If message is nil, the default message for status is used provided by HTTP::Status#description.

Raises IO::Error if the response is closed or headers were already sent.

Source
respond_with_status(status : Int, message : String | Nil = nil) : Nil

Sends status and message as response.

This method calls #reset to remove any previous settings and writes the given status and message to the response IO. Finally, it closes the response.

If message is nil, the default message for status is used provided by HTTP::Status#description.

Raises IO::Error if the response is closed or headers were already sent.

Source
status

The status code of this response, which must be set before writing the response body. If not set, the default value is 200 (OK).

Source
status=(status : HTTP::Status) : HTTP::Status
Source
status_code

Convenience method to retrieve the HTTP status code.

Source
status_code=(status_code : Int32) : Int32

Convenience method to set the HTTP status code.

Source
status_message

Returns the status message.

Defaults to description of #status.

Source
status_message=(status_message : String | Nil) : String

Sets the status message.

Source
upgrade

Upgrades this response, writing headers and yielding the connection IO (a socket) to the given block. This is useful to implement protocol upgrades, such as websockets.

Source
version

The version of the HTTP::Request that created this response.

Source
write(slice : Bytes) : Nil

See IO#write(slice).

Source