Skip to content

class Athena::Framework::StreamedResponse
inherits Athena::Framework::Response #

Represents an ATH::Response whose content should be streamed to the client as opposed to being written all at once. This can be useful in cases where the response content is too large to fit into memory.

The content is stored in a proc that gets called when self is being written to the response IO. How the output gets written can be customized via an ATH::Response::Writer.

Constructors#

.new(status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers | ATH::Response::Headers = ATH::Response::Headers.new, &block : IO -> Nil)#

Creates a new response with optional status, and headers arguments.

The block is captured and called when self is being written to the response's IO. This can be useful to reduce memory overhead when needing to return large responses.

require "athena"

class ExampleController < ATH::Controller
  @[ARTA::Get("/users")]
  def users : ATH::Response
    ATH::StreamedResponse.new headers: HTTP::Headers{"content-type" => "application/json; charset=utf-8"} do |io|
      User.all.to_json io
    end
  end
end

ATH.run

# GET /users # => [{"id":1,...},...]
View source

.new(callback : Proc(IO, Nil), status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers | ATH::Response::Headers = ATH::Response::Headers.new)#

Creates a new response with the provided callback and optional status, and headers arguments.

The proc is called when self is being written to the response's IO.

View source

Methods#

#content=(callback : Proc(IO, Nil))#

Updates the callback of self.

View source