class Hatty::Response
- Hatty::Response
- Reference
- Object
Defined in:
hatty/response.crConstructors
Instance Method Summary
- #cookies : HTTP::Cookies
-
#hatty_send_status_code : Bool
NOTE INTERNAL PROPERTY.
-
#hatty_sent : Bool
NOTE INTERNAL PROPERTY.
- #headers : HTTP::Headers
-
#original : HTTP::Server::Response
Returns the original response
-
#redirect(to location : String) : Nil
Redirects to location.
-
#send(response) : Nil
Sends the response "as-is" to the client.
- #send_file(path : String, filename : String? = nil) : Nil
- #send_file(file : File, filename : String? = nil) : Nil
-
#send_json(json : String) : Nil
Sends the response as
application/json
to the client. -
#send_json(json jsonable) : Nil
Sends the response as
application/json
to the client. -
#send_status(status_code) : Nil
Forwards the request to the status handlers.
-
#send_text(text) : Nil
Sends the text as
text/plain
to the client. - #status_code : Int32
- #status_code=(code)
Constructor Detail
Instance Method Detail
NOTE INTERNAL PROPERTY. This property tells Hatty if it should forward the request to a status handler.
NOTE INTERNAL PROPERTY. This property tells Hatty if the response has been used.
Redirects to location.
get "/article/:id" do |request, response|
response.redirect("/new-article-url/#{request.params["id"]}")
end
Sends the response "as-is" to the client.
get "/" do |request, response|
response.send "Hellooo!"
end
Sends the response as application/json
to the client.
NOTE The response must have a #to_json
method. Hatty automatically requires the JSON module which adds such method to Hash
, NamedTuple
and others.
get "/" do |request, response|
json = {hello: "world"}
response.send_json(json)
end
The client receives
{"hello": "world"}
Forwards the request to the status handlers.
Example: Imaginary admin page with authorization
get "/admin" do |request, response|
authorized = MyApi.valid_token?(request.headers["Authorization"]?)
if !authorized
response.send_status 401
next
end
# ...
end
# `Response#send_status 401` forwards the request to this handler
status 401 do |request, response|
response.send_text "ERR! Unauthorized."
end
Sends the text as text/plain
to the client.
get "/" do |request, response|
response.send_text "FeelsGoodMan"
end
The client receives
FeelsGoodMan