class HTTP::Server::Response

Overview

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.

Defined in:

core_ext/http_server_response.cr:4
core_ext/http_server_response.cr:169

Instance Method Summary

Instance Method Detail

def close : Nil #

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


[View source]
def flush : Nil #

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


[View source]
def 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.


[View source]
def status : HTTP::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).


[View source]
def status=(status : HTTP::Status) #

[View source]
def status_code : Int32 #

Convenience method to retrieve the HTTP status code.


[View source]
def status_code=(status_code : Int32) #

Convenience method to set the HTTP status code.


[View source]
def 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.


[View source]
def write(slice : Bytes) : Nil #

See IO#write(slice).


[View source]