HTTPResponse

public struct HTTPResponse : HTTPMessage

An HTTP response from a server back to the client.

let httpRes = HTTPResponse(status: .ok)

See HTTPClient and HTTPServer.

  • The HTTP version that corresponds to this response.

    Declaration

    Swift

    public var version: HTTPVersion { get set }
  • The HTTP response status.

    Declaration

    Swift

    public var status: HTTPResponseStatus { get set }
  • The header fields for this HTTP response. The "Content-Length" and "Transfer-Encoding" headers will be set automatically when the body property is mutated.

    Declaration

    Swift

    public var headers: HTTPHeaders { get set }
  • 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.

    Declaration

    Swift

    public var body: HTTPBody { get set }
  • If set, reference to the NIO Channel this response came from.

    Declaration

    Swift

    public var channel: Channel?
  • Get and set HTTPCookies for this HTTPResponse This accesses the "Set-Cookie" header.

    Declaration

    Swift

    public var cookies: HTTPCookies { get set }
  • See CustomStringConvertible

    Declaration

    Swift

    public var description: String { get }
  • Creates a new HTTPResponse.

    let httpRes = HTTPResponse(status: .ok)
    

    Declaration

    Swift

    public init(
        status: HTTPResponseStatus = .ok,
        version: HTTPVersion = .init(major: 1, minor: 1),
        headers: HTTPHeaders = .init(),
        body: LosslessHTTPBodyRepresentable = HTTPBody()
    )

    Parameters

    status

    HTTPResponseStatus to use. This defaults to HTTPResponseStatus.ok

    version

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

    headers

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

    body

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