Skip to content

Request

Elliott Minns edited this page Feb 23, 2016 · 5 revisions

Request

The request object represents the HTTP request and has properties for the request query string, parameters, body, HTTP headers, and so on. In this documentation and by convention, the object is always referred to as request (and the HTTP response is response) 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

let request.method: Method

The request method that was used. Can be .POST, .GET, .PUT, .PATCH, .DELETE

var params: [String: String]

The URL parameters that were specified in the request

/user/:id would provide the parameter id, which would be accessible via request.params["id"]

var data: [String: Any]

Any POST or PUT type data, such as JSON or Form data

var cookies: [String: String]

The requests cookies

var path: String

The path of the request

var body: Data

The RAW request body. See the Data struct for more details

var address: String

The Host address of the request

var files: [String: [MultipartFile]]

Any multipart files that were uploaded. These required the Multiparser() middleware to be added into the stack.

var session: Session

The current request session.

Methods

request.getHeader(String) -> String?

Opens up access to the headers of the request. Such as Content-Type

request.setValue(String, forHeader: String)

Allows access to set a new header or overwrite an original one.