HTTPRequest

public struct HTTPRequest : HTTPMessage

An HTTP request from a client to a server.

let httpReq = HTTPRequest(method: .GET, url: "/hello")

See HTTPClient and HTTPServer.

  • The HTTP method for this request.

    httpReq.method = .GET
    

    Declaration

    Swift

    public var method: HTTPMethod { get set }
  • url

    The URL used on this request.

    Declaration

    Swift

    public var url: URL { get set }
  • The unparsed URL string. This is usually set through the url property.

    httpReq.urlString = "/welcome"
    

    Declaration

    Swift

    public var urlString: String { get set }
  • The version for this HTTP request.

    Declaration

    Swift

    public var version: HTTPVersion { get set }
  • The header fields for this HTTP request. 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.

    httpReq.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 request came from.

    Declaration

    Swift

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

    Declaration

    Swift

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

    Declaration

    Swift

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

    let httpReq = HTTPRequest(method: .GET, url: "/hello")
    

    Declaration

    Swift

    public init(
        method: HTTPMethod = .GET,
        url: URLRepresentable = URL.root,
        version: HTTPVersion = .init(major: 1, minor: 1),
        headers: HTTPHeaders = .init(),
        body: LosslessHTTPBodyRepresentable = HTTPBody()
    )

    Parameters

    method

    HTTPMethod to use. This defaults to HTTPMethod.GET.

    url

    A URLRepresentable item that represents the request’s URL. This defaults to "/".

    version

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

    headers

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

    body

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