HTTPMessage

public protocol HTTPMessage : CustomDebugStringConvertible, CustomStringConvertible

An HTTP message. This is the basis of HTTPRequest and HTTPResponse. It has the general structure of:

<status line> HTTP/1.1
Content-Length: 5
Foo: Bar

hello

Note

The status line contains information that differentiates requests and responses. If the status line contains an HTTP method and URI it is a request. If the status line contains an HTTP status code it is a response.

This protocol is useful for adding methods to both requests and responses, such as the ability to serialize content to both message types.

  • The HTTP version of this message.

    Declaration

    Swift

    var version: HTTPVersion { get set }
  • The HTTP headers.

    Declaration

    Swift

    var headers: HTTPHeaders { get set }
  • The optional HTTP body.

    Declaration

    Swift

    var body: HTTPBody { get set }
  • If this message came from an NIO pipeline, the Channel property may be set. Use this to access things like the allocator or address.

    Declaration

    Swift

    var channel: Channel? { get }
  • remotePeer Extension method

    Represents the information we have about the remote peer of this message.

    The peer (remote/client) address is important for availability (block bad clients by their IP) or even security. We can always get the remote IP of the connection from the Channel. However, when clients go through a proxy or a load balancer, we’d like to get the original client’s IP. Most proxy servers and load balancers communicate the information about the original client in certain headers.

    See https://en.wikipedia.org/wiki/X-Forwarded-For

    Declaration

    Swift

    public var remotePeer: HTTPPeer { get }
  • contentType Extension method

    MediaType specified by this message’s "Content-Type" header.

    Declaration

    Swift

    public var contentType: MediaType? { get set }
  • accept Extension method

    Returns a collection of MediaTypePreferences specified by this HTTP message’s "Accept" header.

    You can returns all MediaTypes in this collection to check membership.

    httpReq.accept.mediaTypes.contains(.html)
    

    Or you can compare preferences for two MediaTypes.

    let pref = httpReq.accept.comparePreference(for: .json, to: .html)
    

    Declaration

    Swift

    public var accept: [MediaTypePreference] { get }
  • debugDescription Extension method

    See CustomDebugStringConvertible

    Declaration

    Swift

    public var debugDescription: String { get }