data HTTP::Response(status: Int, headers: HTTP::Headers, body: HTTP::ResponseBody)
What a handler returns. A data value: #header and with make
changed copies. When it sends one, the server adds the framing
(Content-Length or Transfer-Encoding) and a Date header.
res = HTTP::Response.text("hello").header("Cache-Control", "no-store")
res.status # => 200
res.headers["content-type"] # => "text/plain; charset=utf-8"
res.reason # => "OK"
HTTP::Response.redirect("/login").headers["location"] # => "/login"
<std:http>:930
Class methods
def self.empty(status: Int = …, headers: HTTP::Headers = …) -> HTTP::Response
A response without a body.
<std:http>:939
def self.text(body: String, status: Int = …) -> HTTP::Response
A text/plain; charset=utf-8 response.
<std:http>:944
def self.html(body: String, status: Int = …) -> HTTP::Response
A text/html; charset=utf-8 response.
<std:http>:950
def self.json(value: JSON::Value, status: Int = …) -> HTTP::Response
An application/json response, its body value's compact JSON text.
<std:http>:956
def self.bytes(body: Bytes, content_type: String, status: Int = …) -> HTTP::Response
A binary response of type content_type.
<std:http>:962
def self.redirect(location: String, status: Int = …) -> HTTP::Response
A redirection to location (302 Found unless status says
otherwise), with no body. Raises ArgumentError for a location with a
control character.
<std:http>:969
def self.not_found(message: String = …) -> HTTP::Response
A 404 text response.
<std:http>:974
def self.stream(status: Int = …, headers: HTTP::Headers = …, &write: shareable (HTTP::BodyWriter) -> Void) -> HTTP::Response
A response whose body the block writes through a BodyWriter, sent
chunked (unframed to an HTTP/1.0 client, the connection then closed).
The block runs on the connection's task after the headers are sent, so
it may run as long as the client stays; write_timeout applies to each
chunk.
events = HTTP::Headers.of({"Content-Type" => "text/event-stream"})
HTTP::Response.stream(headers: events) do |w|
3.times do |i|
w.write("data: #{i}\n\n")
sleep 1.second
end
end
<std:http>:991
Methods
def status -> Int
The status code, 200 to 599 for the server to send it.
<std:http>:932
def headers -> HTTP::Headers
The header fields.
<std:http>:934
def body -> HTTP::ResponseBody
The body.
<std:http>:936
def to_h -> Hash[Symbol, Object]
The fields as a Hash, by name.
<std:http>:939
def with(status: Int = …, headers: HTTP::Headers = …, body: HTTP::ResponseBody = …) -> HTTP::Response
A copy with the fields given replaced.
<std:http>:939
def header(name: String, value: String) -> HTTP::Response
The response with every value of name replaced by value
(Headers#put).
<std:http>:998
def reason -> String
The status's reason phrase: "OK" for 200 ("" for an unknown one).
<std:http>:1001