-
Notifications
You must be signed in to change notification settings - Fork 39
Response
The response
object represents the HTTP response that a Blackfish app sends when it gets an HTTP request.
In this documentation and by convention, the object is always referred to as response
(and the HTTP request is request
) but its actual name is determined by the parameters to the callback function in which you’re working.
For example:
app.get("/user/:id") { request, response in
response.send(text: 'user ' + request.data["id"])
}
But you could just as well have:
app.get("/user/:id") { req, res in
res.send(text: 'user ' + req.data["id"])
}
The status of the request, as chosen by one of the status enum
public enum Status {
case OK, Created, Accepted, NoContent
case MovedPermanently
case BadRequest, Unauthorized, Forbidden, NotFound
case ServerError
case Unknown
case Custom(Int)
}
The text body to send back with the response.
Sends back a response with whatever configuration has been set on the properties.
Sends a plain text string back with the Content-Type of text/plain
Sends back html with the Content-Type of text/html
Sends back an object in json format with the Content-Type of application/json
app.get("/") { request, response in
let data = ["Hello": "World"]
response.send(json: data)
}
The response will be {"Hello": "World"}
Renders a view object found in the Resources
directory. See [View Engines](View Engine) for more details.
e.g.
app.get("/") { request, response in
response.render("index.html")
}