Vapor Documentation Beta

Class Response

public final class Response: CustomStringConvertible

An HTTP response from a server back to the client.

let httpRes = HTTPResponse(status: .ok)

See HTTPClient and HTTPServer.

%3 Response Response CustomStringConvertible CustomStringConvertible Response->CustomStringConvertible ResponseEncodable ResponseEncodable Response->ResponseEncodable

Nested Types

Response.Body

Represents a Response's body.

Conforms To

ResponseEncodable

Can convert self to a Response.

CustomStringConvertible

Initializers

init(status:​version:​headers:​body:​)

public convenience init(status: HTTPResponseStatus = .ok, version: HTTPVersion = .init(major: 1, minor: 1), headers: HTTPHeaders = .init(), body: Body = .empty)

Creates a new HTTPResponse.

let httpRes = HTTPResponse(status: .ok)

Parameters

status HTTPResponse​Status

HTTPResponseStatus to use. This defaults to HTTPResponseStatus.ok

version HTTPVersion

HTTPVersion of this response, should usually be (and defaults to) 1.1.

headers HTTPHeaders

HTTPHeaders to include with this response. Defaults to empty headers. The "Content-Length" and "Transfer-Encoding" headers will be set automatically.

body Body

HTTPBody for this response, defaults to an empty body. See LosslessHTTPBodyRepresentable for more information.

init(status:​version:​headers​NoUpdate:​body:​)

public init(status: HTTPResponseStatus, version: HTTPVersion, headersNoUpdate headers: HTTPHeaders, body: Body)

Internal init that creates a new HTTPResponse without sanitizing headers.

Properties

version

var version: HTTPVersion

The HTTP version that corresponds to this response.

status

var status: HTTPResponseStatus

The HTTP response status.

headers

var headers: HTTPHeaders

The header fields for this HTTP response. The "Content-Length" and "Transfer-Encoding" headers will be set automatically when the body property is mutated.

body

var body: Body

The HTTPBody. Updating this property will also update the associated transport headers.

httpRes.body = HTTPBody(string: "Hello, world!")

Also be sure to set this message's contentType property to a MediaType that correctly represents the HTTPBody.

storage

var storage: Storage

cookies

var cookies: HTTPCookies

Get and set HTTPCookies for this HTTPResponse This accesses the "Set-Cookie" header.

description

var description: String

See CustomStringConvertible

content

var content: ContentContainer

Methods

encode​Response(for:​)

public func encodeResponse(for request: Request) -> EventLoopFuture<Response>

See HTTPResponseCodable.