Vapor Documentation

Class Response

public final class Response: CustomStringConvertible  

An HTTP response from a server back to the client.

let res = Response(status: .ok)

See HTTPClient and HTTPServer.

%35 Response Response ResponseEncodable ResponseEncodable Response->ResponseEncodable CustomStringConvertible CustomStringConvertible Response->CustomStringConvertible AsyncResponseEncodable AsyncResponseEncodable Response->AsyncResponseEncodable

Nested Types

Response.Body

Represents a Response's body.

Conforms To

AsyncResponseEncodable

Can convert self to a Response.

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 Response.

let res = Response(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

Body for this response, defaults to an empty body. See Response.Body 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 Response without sanitizing headers.

Properties

version

public var version: HTTPVersion

The HTTP version that corresponds to this response.

status

public var status: HTTPResponseStatus

The HTTP response status.

headers

public 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

public var body: Body  

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

res.body = Response.Body(string: "Hello, world!")

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

upgrader

public var upgrader: Upgrader? 

Optional Upgrade behavior to apply to this response. currently, websocket upgrades are the only defined case.

storage

public var storage: Storage

cookies

public var cookies: HTTPCookies  

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

description

public var description: String  

See CustomStringConvertible

content

public var content: ContentContainer  

Methods

encode​Response(for:​)

@available(macOS 12, iOS 15, watchOS 8, tvOS 15, *)
    public func encodeResponse(for request: Request) async throws -> Response  

encode​Response(for:​)

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