Skip to content

class Athena::Routing::StreamedResponse
inherits Athena::Routing::Response #

Represents an ART::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 ART::Response::Writer.

Class methods#

.new(status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers | ART::Response::Headers = ART::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 < ART::Controller
  @[ARTA::Get("/users")]
  def users : ART::Response
    ART::StreamedResponse.new headers: HTTP::Headers{"content-type" => "application/json; charset=UTF-8"} do |io|
      User.all.to_json io
    end
  end
end

ART.run

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

.new(callback : Proc(IO, Nil), status : HTTP::Status | Int32 = HTTP::Status::OK, headers : HTTP::Headers | ART::Response::Headers = ART::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