Skip to content

Response

Josh Wright edited this page Jan 14, 2021 · 4 revisions

Response

A type representing the response from an HTTP endpoint. This response can be a failure or success case depending on the status code in the head.

public final class Response

Inheritance

ResponseConvertible

Initializers

init(status:headers:body:)

Creates a new response using a status code, headers and body. If the headers do not contain content-length or content-type, those will be appended based on the supplied HTTPBody.

public init(status: HTTPResponseStatus, headers: HTTPHeaders = HTTPHeaders(), body: HTTPBody?)

Parameters

  • status: The status code of this response.
  • headers: Any headers to return in the response. Defaults to empty headers.
  • body: The body of this response. See HTTPBody for initializing with various data.

init(_:)

Initialize this response with a closure that will be called, allowing you to directly write headers, body, and end to the response. The request connection will be left open until you .writeEnd() to the closure's ResponseWriter.

public init(_ writer: @escaping (ResponseWriter) -> Void)

Usage:

app.get("/stream") {
    Response { writer in
        writer.writeHead(...)
        writer.writeBody(...)
        writer.writeEnd()
    }
}

Parameters

  • writer: A closure take a ResponseWriter and using it to write response data to a remote peer.

Properties

defaultJSONEncoder

The default JSONEncoder with which to encode JSON responses.

var defaultJSONEncoder

defaultErrorResponse

The default response for when there is an error along the routing chain that does not conform to ResponseConvertible.

var defaultErrorResponse

status

The success or failure status response code.

var status: HTTPResponseStatus

headers

The HTTP headers.

var headers: HTTPHeaders

body

The body which contains any data you want to send back to the client This can be HTML, an image or JSON among many other data types.

let body: HTTPBody?

Methods

convert()

public func convert() throws -> EventLoopFuture<Response>
Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally