class Tiny::Response
- Tiny::Response
- Reference
- Object
Overview
A facade which sits in front of HTTP::Server::Response,
providing a few helper methods. The object is passed into the block of
Tiny's serve
method.
serve do |request, response|
# Return text to the client
response.send "Unicorns!"
# Return text with a specific status code
response.send 500, "An error occurred"
# Return a JSON object
response.json {
"testing" => "test"
}
end
Defined in:
tiny/response.crConstructors
-
.new(context : HTTP::Server::Context)
Create the response object
Instance Method Summary
-
#allow_header(header : String)
Set the
Access-Control-Allow-Headers
header -
#allow_origin(hostname : String?)
Set the
Access-Control-Allow-Origin
header -
#allowed_headers : Array(String)
A list of allowed headers for CORS purposes
-
#error(code : Int32, message : String)
Send an error response with a specific status code
-
#error(message : String)
Send an error response with a 500 status code
-
#json(code : Int32, object : JSON::Type)
Send a JSON response with a specific status code
-
#json(object : JSON::Type)
Send a JSON response with a 200 status code
-
#request_methods(methods : Array(String))
Set the
Access-Control-Request-Method
header -
#send(code : Int32)
Send an empty response with a specific status code
-
#send(code : Int32, *message : String)
Send a response with a 200 status code
-
#send(*message : String)
Send a response with a 200 status code
Macro Summary
-
method_missing(call)
Cascade missing methods down to the server response
Constructor Detail
Instance Method Detail
Set the Access-Control-Allow-Origin
header
response.allow_origin "example.com"
Send an error response with a specific status code
response.error 403, "Something happened"
Send an error response with a 500 status code
response.error "Something happened"
Send a JSON response with a specific status code
response.json 500, {
"error": "Something happened"
}
Send a JSON response with a 200 status code
response.json {
"rainbows": "unicorns"
}
Set the Access-Control-Request-Method
header
response.request_methods ["GET", "POST"]
Send a response with a 200 status code
response.send 403, "You're not allowed to do that"
Multi-line responses can be sent by passing multiple strings
response.send 403,
"Oh, crumbs!",
"You're not allowed to do that"
Send a response with a 200 status code
response.send "Tada!"
Multi-line responses can be sent by passing multiple strings
response.send
"First Line",
"Second Line",
"Third Line"