RouterRequest
public class RouterRequest
The RouterRequest
class is used to interact with incoming HTTP requests to the Router.
It contains and allows access to the request’s Headers
and Body
as well as other properties of the request.
It can also perform content negotiation based on the request’s “Accept” header.
Usage Example:
In this example “request” is an instance of the class RouterRequest
.
It is used by the server to read the body of the request as a String and send it back to the user.
let router = Router()
router.post("/") { request, response, next in
let body = request.readString()
response.send(body)
next()
}
-
The hostname of the request.
Declaration
Swift
public var hostname: String { get }
-
The port of the request.
Declaration
Swift
public var port: Int { get }
-
The domain name of the request.
Declaration
Swift
public private(set) lazy var domain: String { get set }
-
The subdomains string array of request.
Declaration
Swift
public private(set) lazy var subdomains: [String] { get set }
-
The HTTP version of the request.
Declaration
Swift
public let httpVersion: HTTPVersion
-
The method of the request.
Declaration
Swift
public let method: RouterMethod
-
The router as a String.
Declaration
Swift
public internal(set) var route: String? { get }
-
IP address string of server.
Declaration
Swift
public var remoteAddress: String { get }
-
The parsed URL.
Declaration
Swift
public private(set) lazy var parsedURL: URLParser { get set }
-
The currently matched section of the URL.
Declaration
Swift
public internal(set) var matchedPath: String { get }
-
The original URL as a string.
Declaration
Swift
public var originalURL: String { get }
-
The URL. This contains just the path and query parameters starting with ‘/’ Use ‘urlURL’ for the full URL
Declaration
Swift
@available(*, deprecated, message: "This contains just the path and query parameters starting with '/'. use 'urlURL' instead") public var url: String { get }
-
The URL from the request as URLComponents URLComponents has a memory leak on linux as of swift 3.0.1. Use ‘urlURL’ instead
Declaration
Swift
@available(*, deprecated, message: "URLComponents has a memory leak on linux as of swift 3.0.1. use 'urlURL' instead") public var urlComponents: URLComponents { get }
-
The URL from the request
Declaration
Swift
public var urlURL: URL { get }
-
List of URL parameters.
Declaration
Swift
public internal(set) var parameters: [String : String] { get }
-
List of HTTP headers with simple String values.
Declaration
Swift
public let headers: Headers
-
Parsed Cookies, used to do a lazy parsing of the appropriate headers.
Declaration
Swift
public lazy var cookies: [String : HTTPCookie] { get set }
-
List of query parameters and comma-separated values.
Declaration
Swift
public lazy var queryParameters: [String : String] { get set }
-
Query parameters with values as an array.
Declaration
Swift
public lazy var queryParametersMultiValues: [String : [String]] { get set }
-
Convert query parameters into a QueryParam type
Declaration
Swift
public func getQueryParameters<T>(as type: T.Type) -> T? where T : QueryParams
Parameters
type
The QueryParam type describing the expected query parameters
Return Value
The route’s Query parameters as a QueryParam object
-
User info. Can be used by middlewares and handlers to store and pass information on to subsequent handlers.
Declaration
Swift
public var userInfo: [String : Any]
-
Body of the message.
Declaration
Swift
public internal(set) var body: ParsedBody? { get }
-
Read the body of the request as Data.
Throws
Socket.Error if an error occurred while reading from a socket.Declaration
Swift
public func read(into data: inout Data) throws -> Int
Parameters
into
Data object in which the body of the request is returned.
Return Value
the number of bytes read.
-
Read the body of the request as a Codable object using a
BodyDecoder
that was selected based on the Content-Type header. Defaults toJSONDecoder()
if no decoder is provided.Usage Example:
The example below defines a
User
struct and then decodes aUser
from the body of a request.public struct User: Codable { let name: String } let router = Router() router.post("/example") { request, response, next in let user = try request.read(as: User.self) print(user.name) next() }
Throws
Socket.Error if an error occurred while reading from a socket.Throws
DecodingError.dataCorrupted
if values requested from the payload are corrupted, or if the given data is not valid JSON.Throws
An error if any value throws an error during decoding.Declaration
Swift
public func read<T>(as type: T.Type) throws -> T where T : Decodable
Parameters
as
Codable object to which the body of the request will be converted.
Return Value
The instantiated Codable object
-
Read the body of the request as String.
Throws
Socket.Error if an error occurred while reading from a socket.Declaration
Swift
public func readString() throws -> String?
Return Value
the String with the request body.
-
Check if passed in types are acceptable based on the request’s header field specified in the first parameter.
Declaration
Swift
public func accepts(header: String = "Accept", types: [String]) -> String?
Parameters
header
name of request’s header field to be checked.
types
array of content/mime type strings.
Return Value
most acceptable type or nil if there are none.
-
Check if passed in types are acceptable based on the request’s header field specified in the first parameter.
Declaration
Swift
public func accepts(header: String = "Accept", types: String...) -> String?
Parameters
header
name of request’s header field to be checked.
types
content/mime type strings.
Return Value
most acceptable type or nil if there are none.
-
Check if passed in types are acceptable based on the request’s header field specified in the first parameter.
Declaration
Swift
public func accepts(header: String = "Accept", type: String) -> String?
Parameters
header
name of request’s header field to be checked.
type
content/mime type string.
Return Value
most acceptable type or nil if there are none.