1.9 KiB
Request
When a client connects with an HTTP Server it sends a Request. This HTTP request will be processed as discussed here and resolved into a Response. This is the response in the http Request/Response model.
Requests consist of a Method, URI and Headers.
Requests can optionally also contain a Body.
Requests are Extensible using the extend property. This allow storing additional data for use by integrating libraries.
Request properties
Request has a few primary properties according to spec, and one extra property as part of the Vapor framework.
The Request Method, URI, Headers and Body are available.
We also provide access to the HTTP version, although this will almost always be 1.1.
In addition to these properties there is an Extend available which an be used to store extra information for each request.
It can be used to store information between middlewares and the responder and is used by Vapor to store the current Worker, too.
Using Extend, many properties can be added in extensions.
Creating a Request
Creating requests is necessary for HTTP Clients.
A request accepts a method, uri, version, headers and body. The version's default is recommended. The body is optional.
The body can be a Body or BodyRepresentable. If the body is a BodyRepresentable the Response initializer will become throwing.
let request1 = Request(
method: .get,
uri: uri,
headers: headers,
body: body
)
let request2 = try Request(
method: .get,
uri: uri,
headers: headers,
body: bodyRepresentable
)