Skip to content

class HTTP::Server::Response
inherits IO

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.

Methods

#close

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

View source

#closed?

Returns true if this response has been closed.

View source

#content_length=(content_length : Int)

Convenience method to set the Content-Length header.

View source

#content_type=(content_type : String)

Convenience method to set the Content-Type header.

View source

#cookies

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

View source

#flush

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

View source

#headers : HTTP::Headers

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

View source

#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).

View 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).

View source

#respond_with_status(status : HTTP::Status, message : String? = 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

#respond_with_status(status : Int, message : String? = 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

#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

#status=(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

#status_code

Convenience method to retrieve the HTTP status code.

View source

#status_code=(status_code : Int32)

Convenience method to set the HTTP status code.

View source

#upgrade(&block : IO -> )

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

#version : String

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

View source

#write(slice : Bytes) : Nil

View source