Skip to content

Request

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

Request

A simplified Request type as you'll come across in many web frameworks

public final class Request

Inheritance

DecodableRequest

Properties

defaultJSONDecoder

The default JSONDecoder with which to decode HTTP request bodies.

var defaultJSONDecoder

head

The head contains all request "metadata" like the URI and request method.

let head: HTTPRequestHead

The headers are also found in the head, and they are often used to describe the body as well.

components

The url components of this request.

let components: URLComponents?

pathParameters

The any parameters inside the path.

var pathParameters: [PathParameter] = []

method

The HTTPMethod of the request.

var method: HTTPMethod

path

The path of the request. Does not include the query string.

var path: String

headers

Any headers associated with the request.

var headers: HTTPHeaders

queryItems

Any query items parsed from the URL. These are not percent encoded.

var queryItems: [URLQueryItem]

body

The body is a wrapper used to provide simple access to any body data, such as JSON.

var body: HTTPBody?

Methods

header(for:)

public func header(for key: String) -> String?

query(for:)

public func query(for key: String) -> String?

pathComponent(for:)

public func pathComponent(for key: String) -> String?

decodeBody(encoding:)

public func decodeBody<T>(encoding: BodyEncoding = .json) throws -> T where T: Decodable

getAuth()

Get any authorization data from the request's Authorization header.

public func getAuth() -> HTTPAuth?

Returns

An HTTPAuth representing relevant info in the Authorization header, if it exists. Currently only supports Basic and Bearer auth.

basicAuth()

Gets any Basic authorization data from this request.

public func basicAuth() -> HTTPAuth.Basic?

Returns

The data from the Authorization header, if the authorization type is Basic.

bearerAuth()

Gets any Bearer authorization data from this request.

public func bearerAuth() -> HTTPAuth.Bearer?

Returns

The data from the Authorization header, if the authorization type is Bearer.

pathParameter(named:)

Returns the first PathParameter for the given key, if there is one.

public func pathParameter(named key: String) -> PathParameter?

Use this to fetch any parameters from the path.

app.post("/users/:user_id") { request in
    let theUserID = request.pathParameter(named: "user_id")?.stringValue
    ...
}

set(_:)

Sets a value associated with this request. Useful for setting objects with middleware.

@discardableResult public func set<T>(_ value: T) -> Self

Usage:

struct ExampleMiddleware: Middleware {
    func intercept(_ request: Request) -> EventLoopFuture<Request> {
        let someData: SomeData = ...
        request.set(someData)
        return .new(value: request)
    }
}

app
    .use(ExampleMiddleware())
    .on(.GET, at: "/example") { request in
        let theData = try request.get(SomeData.self)
    }

Parameters

  • value: The value to set.

Returns

self, with the new value set internally for access with self.get(Value.self).

get(_:)

Gets a value associated with this request, throws if there is not a value of type T already set.

public func get<T>(_ type: T.Type = T.self) throws -> T

Parameters

  • type: The type of the associated value to get from the request.

Throws

An AssociatedValueError if there isn't a value of type T found associated with the request.

Returns

The value of type T from the request.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally