Back to Kitura

RouterRequest

docs/docsets/Kitura.docset/Contents/Resources/Documents/Classes/RouterRequest.html

3.0.19.0 KB
Original Source

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()
}

Properties

`

                hostname
                `

The hostname of the request.

Declaration

Swift

public var hostname: String { get }

`

                port
                `

The port of the request.

Declaration

Swift

public var port: Int { get }

`

                domain
                `

The domain name of the request.

Declaration

Swift

public private(set) lazy var domain: String { get set }

`

                subdomains
                `

The subdomains string array of request.

Declaration

Swift

public private(set) lazy var subdomains: [String] { get set }

`

                httpVersion
                `

The HTTP version of the request.

Declaration

Swift

public let httpVersion: HTTPVersion

`

                method
                `

The method of the request.

Declaration

Swift

public let method: RouterMethod

`

                route
                `

The router as a String.

Declaration

Swift

public internal(set) var route: String? { get }

`

                remoteAddress
                `

IP address string of server.

Declaration

Swift

public var remoteAddress: String { get }

URL

`

                parsedURL
                `

The parsed URL.

Declaration

Swift

public private(set) lazy var parsedURL: URLParser { get set }

`

                matchedPath
                `

The currently matched section of the URL.

Declaration

Swift

public internal(set) var matchedPath: String { get }

`

                originalURL
                `

The original URL as a string.

Declaration

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

Declaration

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

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 }

`

                urlURL
                `

The URL from the request

Declaration

Swift

public var urlURL: URL { get }

`

                parameters
                `

List of URL parameters.

Declaration

Swift

public internal(set) var parameters: [String : String] { get }

Headers

`

                headers
                `

List of HTTP headers with simple String values.

Declaration

Swift

public let headers: Headers

`

                cookies
                `

Parsed Cookies, used to do a lazy parsing of the appropriate headers.

Declaration

Swift

public lazy var cookies: [String : HTTPCookie] { get set }

Query parameters

`

                queryParameters
                `

List of query parameters and comma-separated values.

Declaration

Swift

public lazy var queryParameters: [String : String] { get set }

`

                queryParametersMultiValues
                `

Query parameters with values as an array.

Declaration

Swift

public lazy var queryParametersMultiValues: [String : [String]] { get set }

`

                getQueryParameters(as:)
                `

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

Shared dictionary

`

                userInfo
                `

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]

Request Body

`

                body
                `

Body of the message.

Declaration

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.

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(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.

Usage Example:

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.

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

`

                readString()
                `

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.

Accepts

`

                accepts(header:types:)
                `

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.

`

                accepts(header:types:)
                `

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.

`

                accepts(header:type:)
                `

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.