Vapor Documentation

Protocol Async​Response​Encodable

public protocol AsyncResponseEncodable  

Can convert self to a Response.

Types that conform to this protocol can be returned in route closures.

This is the async version of ResponseEncodable

%239 AsyncResponseEncodable AsyncResponseEncodable ClientResponse ClientResponse ClientResponse->AsyncResponseEncodable View View View->AsyncResponseEncodable Response Response Response->AsyncResponseEncodable Content Content Content->AsyncResponseEncodable AnyAsyncResponse AnyAsyncResponse AnyAsyncResponse->AsyncResponseEncodable

Types Conforming to Async​Response​Encodable

ClientResponse
AnyAsyncResponse

A type erased response useful for routes that can return more than one type.

Content

Convertible to / from content in an HTTP message.

Response

An HTTP response from a server back to the client.

View

Default Implementations

encode​Response(status:​headers:​for:​)

public func encodeResponse(status: HTTPStatus, headers: HTTPHeaders = [:], for request: Request) async throws -> Response  

Asynchronously encodes Self into a Response, setting the supplied status and headers.

router.post("users") { req async throws -> HTTPResponse in
    return try await req.content
        .decode(User.self)
        .save(on: req)
        .encode(status: .created, for: req)
}

Parameters

status HTTPStatus

HTTPStatus to set on the Response.

headers HTTPHeaders

HTTPHeaders to merge into the Response's headers.

Returns

Newly encoded Response.

Requirements

encode​Response(for:​)

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