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
flush

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

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
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)
Source
status_code

Convenience method to retrieve the HTTP status code.

Source
status_code=(status_code : Int32)

Convenience method to set the HTTP status code.

Source
upgrade(protocol : String | Nil = nil, &) : Nil

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
write(slice : Bytes) : Nil

See IO#write(slice).

Source