docs/docsets/Kitura.docset/Contents/Resources/Documents/Classes/RouterRequest.html
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.
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()
}
Properties
`
hostname
`
The hostname of the request.
Swift
public var hostname: String { get }
`
port
`
The port of the request.
Swift
public var port: Int { get }
`
domain
`
The domain name of the request.
Swift
public private(set) lazy var domain: String { get set }
`
subdomains
`
The subdomains string array of request.
Swift
public private(set) lazy var subdomains: [String] { get set }
`
httpVersion
`
The HTTP version of the request.
Swift
public let httpVersion: HTTPVersion
`
method
`
The method of the request.
Swift
public let method: RouterMethod
`
route
`
The router as a String.
Swift
public internal(set) var route: String? { get }
`
remoteAddress
`
IP address string of server.
Swift
public var remoteAddress: String { get }
URL
`
parsedURL
`
The parsed URL.
Swift
public private(set) lazy var parsedURL: URLParser { get set }
`
matchedPath
`
The currently matched section of the URL.
Swift
public internal(set) var matchedPath: String { get }
`
originalURL
`
The original URL as a string.
Swift
public var originalURL: String { get }
`
url
`
The URL. This contains just the path and query parameters starting with ‘/’ Use ‘urlURL’ for the full URL
Swift
@available(*, deprecated, message: "This contains just the path and query parameters starting with '/'. use 'urlURL' instead")
public var url: String { get }
`
urlComponents
`
The URL from the request as URLComponents URLComponents has a memory leak on linux as of swift 3.0.1. Use ‘urlURL’ instead
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 }
`
urlURL
`
The URL from the request
Swift
public var urlURL: URL { get }
`
parameters
`
List of URL parameters.
Swift
public internal(set) var parameters: [String : String] { get }
Headers
`
headers
`
List of HTTP headers with simple String values.
Swift
public let headers: Headers
`
cookies
`
Parsed Cookies, used to do a lazy parsing of the appropriate headers.
Swift
public lazy var cookies: [String : HTTPCookie] { get set }
Query parameters
`
queryParameters
`
List of query parameters and comma-separated values.
Swift
public lazy var queryParameters: [String : String] { get set }
`
queryParametersMultiValues
`
Query parameters with values as an array.
Swift
public lazy var queryParametersMultiValues: [String : [String]] { get set }
`
getQueryParameters(as:)
`
Convert query parameters into a QueryParam type
Swift
public func getQueryParameters<T>(as type: T.Type) -> T? where T : QueryParams
| type |
The QueryParam type describing the expected query parameters
|
The route’s Query parameters as a QueryParam object
Shared dictionary
`
userInfo
`
User info. Can be used by middlewares and handlers to store and pass information on to subsequent handlers.
Swift
public var userInfo: [String : Any]
Request Body
`
body
`
Body of the message.
Swift
public internal(set) var body: ParsedBody? { get }
`
read(into:)
`
Read the body of the request as Data.
Throws
Socket.Error if an error occurred while reading from a socket.
Swift
public func read(into data: inout Data) throws -> Int
| into |
Data object in which the body of the request is returned.
|
the number of bytes read.
`
read(as:)
`
Read the body of the request as a Codable object using a BodyDecoder that was selected based on the Content-Type header. Defaults to JSONDecoder() if no decoder is provided.
The example below defines a User struct and then decodes a User 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.
Swift
public func read<T>(as type: T.Type) throws -> T where T : Decodable
| as |
Codable object to which the body of the request will be converted.
|
The instantiated Codable object
`
readString()
`
Read the body of the request as String.
Throws
Socket.Error if an error occurred while reading from a socket.
Swift
public func readString() throws -> String?
the String with the request body.
Accepts
`
accepts(header:types:)
`
Check if passed in types are acceptable based on the request’s header field specified in the first parameter.
Swift
public func accepts(header: String = "Accept", types: [String]) -> String?
| header |
name of request’s header field to be checked.
|
| types |
array of content/mime type strings.
|
most acceptable type or nil if there are none.
`
accepts(header:types:)
`
Check if passed in types are acceptable based on the request’s header field specified in the first parameter.
Swift
public func accepts(header: String = "Accept", types: String...) -> String?
| header |
name of request’s header field to be checked.
|
| types |
content/mime type strings.
|
most acceptable type or nil if there are none.
`
accepts(header:type:)
`
Check if passed in types are acceptable based on the request’s header field specified in the first parameter.
Swift
public func accepts(header: String = "Accept", type: String) -> String?
| header |
name of request’s header field to be checked.
|
| type |
content/mime type string.
|
most acceptable type or nil if there are none.