-
Notifications
You must be signed in to change notification settings - Fork 14
Request
A simplified Request type as you'll come across in many web frameworks
public final class Request
DecodableRequest
The default JSONDecoder with which to decode HTTP request bodies.
var defaultJSONDecoder
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.
The url components of this request.
let components: URLComponents?
The any parameters inside the path.
var pathParameters: [PathParameter] = []
The HTTPMethod of the request.
var method: HTTPMethod
The path of the request. Does not include the query string.
var path: String
Any headers associated with the request.
var headers: HTTPHeaders
Any query items parsed from the URL. These are not percent encoded.
var queryItems: [URLQueryItem]
The body is a wrapper used to provide simple access to any body data, such as JSON.
var body: HTTPBody?
public func header(for key: String) -> String?
public func query(for key: String) -> String?
public func pathComponent(for key: String) -> String?
public func decodeBody<T>(encoding: BodyEncoding = .json) throws -> T where T: Decodable
Get any authorization data from the request's Authorization
header.
public func getAuth() -> HTTPAuth?
An HTTPAuth
representing relevant info in the Authorization
header, if it exists. Currently only supports Basic
and Bearer
auth.
Gets any Basic
authorization data from this request.
public func basicAuth() -> HTTPAuth.Basic?
The data from the Authorization
header, if the authorization type is Basic
.
Gets any Bearer
authorization data from this request.
public func bearerAuth() -> HTTPAuth.Bearer?
The data from the Authorization
header, if the authorization type is Bearer
.
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
...
}
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)
}
- value: The value to set.
self
, with the new value set internally for access with self.get(Value.self)
.
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
- type: The type of the associated value to get from the request.
An AssociatedValueError
if there isn't a value of type T
found associated with the request.
The value of type T
from the request.
Generated at 2021-01-13T22:24:59-0800 using swift-doc 1.0.0-beta.5.
Alchemy
Types
- AlterTableBuilder
- BCryptDigest
- BasicAuthMiddleware
- BcryptError
- BelongsToRelationship
- CORSMiddleware
- CORSMiddleware.AllowOriginSetting
- CORSMiddleware.Configuration
- ColumnType
- CreateColumn
- CreateColumnBuilder
- CreateIndex
- CreateTableBuilder
- DatabaseConfig
- DatabaseError
- DatabaseField
- DatabaseKeyMappingStrategy
- DatabaseValue
- DayUnit
- Env
- FrequencyTyped
- Grammar
- HTTPAuth
- HTTPAuth.Basic
- HTTPAuth.Bearer
- HTTPBody
- HTTPError
- HasManyRelationship
- HasOneRelationship
- HasRelationship
- HourUnit
- JoinClause
- JoinType
- Launch
- Log
- MIMEType
- MinuteUnit
- ModelQuery
- MySQLDatabase
- Operator
- OrderClause
- OrderClause.Sort
- OrderedDictionary
- PapyrusClientError
- PathParameter
- PathParameter.DecodingError
- PostgresDatabase
- Query
- Request
- Response
- Router
- RuneError
- SQL
- SQLJSON
- Scheduler
- Schema
- SecondUnit
- Services
- Socket
- StaticFileMiddleware
- StringLength
- Thread
- TokenAuthMiddleware
- WeekUnit
- Weekday
- WhereBoolean
- WhereColumn
- WhereIn
- WhereIn.InType
- WhereNested
- WhereRaw
- WhereValue