2.3 KiB
HTTP Response
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.
HTTP's Response object contains a Status, Headers and a Body. Before further reading this page, you must have read and understood the previous pages for Status, Headers and Body.
Responses are Extensible using the extend property. This allow storing additional data for use by integrating libraries.
Creating a Response
A Response accepts a version, status, 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 response1 = Response(
status: status,
headers: headers,
body: body)
let response2 = try Response(
status: status,
headers: headers,
body: bodyRepresentable)
ResponseRepresentable
Instead of requiring a Response, many parts of the framework and related libraries work with the protocol ResponseRepresentable. When types conform to ResponseRepresentable they're required to implement a makeResponse function that allows conversion from this instance to a Response.
For the purpose of an example, we'll convert an integer to a Response. This Int will always response with a status code 200 (OK) and a body containing itself in textual representation.
extension Int: ResponseRepresentable {
public func makeResponse() throws -> Response {
return try Response(status: .ok, body: self.description)
}
}
ResponseInitializable
ResponseInitializable is used for converting a Response to another type.
This is particularly useful for HTTP Clients that interact with existing APIs.
This example is pseudocode for interacting with a payment API such as Stripe or PayPal in a type-safe fashion.
struct PaymentStatus: ResponseInitializable {
public init(response: Response) throws {
// Create a `PaymentStatus` from the API call's `Response`
}
}