class Hatty::Response

Defined in:

hatty/response.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(response : HTTP::Server::Response) #

[View source]

Instance Method Detail

def cookies : HTTP::Cookies #

[View source]
def hatty_send_status_code : Bool #

NOTE INTERNAL PROPERTY. This property tells Hatty if it should forward the request to a status handler.


[View source]
def hatty_sent : Bool #

NOTE INTERNAL PROPERTY. This property tells Hatty if the response has been used.


[View source]
def headers : HTTP::Headers #

[View source]
def original : HTTP::Server::Response #

Returns the original response


[View source]
def redirect(to location : String) : Nil #

Redirects to location.

get "/article/:id" do |request, response|
  response.redirect("/new-article-url/#{request.params["id"]}")
end

[View source]
def send(response) : Nil #

Sends the response "as-is" to the client.

get "/" do |request, response|
  response.send "Hellooo!"
end

[View source]
def send_file(path : String, filename : String? = nil) : Nil #

[View source]
def send_file(file : File, filename : String? = nil) : Nil #

[View source]
def send_json(json : String) : Nil #

Sends the response as application/json to the client.


[View source]
def send_json(json jsonable) : Nil #

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"}

[View source]
def send_status(status_code) : Nil #

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

[View source]
def send_text(text) : Nil #

Sends the text as text/plain to the client.

get "/" do |request, response|
  response.send_text "FeelsGoodMan"
end

The client receives

FeelsGoodMan

[View source]
def status_code : Int32 #

[View source]
def status_code=(code) #

[View source]