Skip to content

Response

Elliott Minns edited this page Feb 22, 2016 · 6 revisions

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

Properties

response.status - Status

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

response.text - String

The text body to send back with the response.

Methods

response.send()

Sends back a response with whatever configuration has been set on the properties.

response.send(text: String)

Sends a plain text string back with the Content-Type of text/plain

response.send(html: String)

Sends back html with the Content-Type of text/html

response.send(json: Any)

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

response.render(String)

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

Roadmap

Guide

Clone this wiki locally