Module: import HTTP
Request
類是我們和HTTP模塊打交道最多的部分,喜愛(ài)滿看一些它常用的屬性:
public var method: Method
public var uri: URI
public var parameters: Node
public var headers: [HeaderKey: String]
public var body: Body
public var data: Content
Method
HTTP請(qǐng)求方法包括:GET毅该、POST、PUT、PATCH顿涣、DELETE。
URI
與請(qǐng)求相關(guān)的URI
類酝豪,用來(lái)訪問(wèn)發(fā)送請(qǐng)求的uri
的屬性涛碑。
比如:http://vapor.codes/example?query=hi#fragments-too
let scheme = request.uri.scheme // http
let host = request.uri.host // vapor.codes
let path = request.uri.path // /example
let query = request.uri.query // query=hi
let fragment = request.uri.fragment // fragments-too
Route Parameters
請(qǐng)求相關(guān)的url 參數(shù)。比如我們注冊(cè)了hello/:name/age/:age
的路徑孵淘,那么我們可以獲取如下內(nèi)容:
let name = request.parameters["name"] // String?
let age = request.parameters["age"]?.int // Int?
或者你也可以用extract
拋出nil
和無(wú)效的變量蒲障。
let name = try request.parameters.extract("name") as String
let age = try request.parameters.extract("age") as Int
extract
函數(shù)可以轉(zhuǎn)換任何NodeInitializable
類型,包括自定義的類型瘫证。如果要了解更多請(qǐng)參考Node
注意:路由部分也說(shuō)明了類型安全的路由
Headers
請(qǐng)求相關(guān)的頭部信息揉阎。如果你準(zhǔn)備對(duì)外發(fā)送請(qǐng)求,你可以將自己的秘鑰添加到headers
中背捌。
let contentType = request.headers["Content-Type"]
向外部發(fā)送請(qǐng)求時(shí):
let request = Request ...
request.headers["Content-Type"] = "application/json"
request.headers["Authorization"] = ... my auth token
Extending Headers
我們總是盡可能的剔除字符串類型的代碼來(lái)提高程序代碼毙籽。我們可以通過(guò)擴(kuò)展向headers
中添加變量。
extension HTTP.KeyAccessible where Key == HeaderKey, Value == String {
var customKey: String? {
get {
return self["Custom-Key"]
}
set {
self["Custom-Key"] = newValue
}
}
}
實(shí)現(xiàn)上面的模式之后毡庆,我們的字符串類型的Custom-key
包含在了我們的代碼里】由模現(xiàn)在可以如下訪問(wèn):
let customKey = request.headers.customKey
// or
let request = ...
request.headers.customKey = "my custom value"
Body
body
是請(qǐng)求相關(guān)的主體,承載這主要數(shù)據(jù)么抗。在body可以查看更多信息毅否。
為了傳到請(qǐng)求中,我們經(jīng)常這樣拉取body字節(jié):
let rawBytes = request.body.bytes
Content
通常乖坠,當(dāng)我們發(fā)送或接收請(qǐng)求時(shí)搀突,我們將其用作傳輸內(nèi)容的方式。 為此熊泵,Vapor提供了一個(gè)方便的data
變量仰迁,與請(qǐng)求相關(guān)聯(lián),以一致的方式管理請(qǐng)求內(nèi)容顽分。
比如徐许,我從http://vapor.codes?hello=world
接收一個(gè)請(qǐng)求:
let world = request.data["hello"]?.string
如果我們接收以下json數(shù)據(jù),上面的代碼同樣適用:
{
"hello": "world"
}
這也適用于"multi-part"請(qǐng)求卒蘸,甚至可以通過(guò)Middleware
擴(kuò)展新的類型雌隅,如XML或YAML翻默。
如果你喜歡更明確地訪問(wèn)給定的類型,那是完全正確的恰起。 data
變量純粹為開(kāi)發(fā)者選擇方便修械。
JSON
可以直接通過(guò)json獲取請(qǐng)求的json
數(shù)據(jù):
let json = request.json["hello"]
Query Parameters
query方法獲取查詢數(shù)據(jù):
let query = request.query?["hello"] // String?
let name = request.query?["name"]?.string // String?
let age = request.query?["age"]?.int // Int?
let rating = request.query?["rating"]?.double // Double?
Key Paths
Key paths 適用于大多數(shù)Vapor類型的key-value嵌套。
如下json示例:
{
"metadata": "some metadata",
"artists" : {
"href": "http://someurl.com",
"items": [
{
"name": "Van Gogh",
},
{
"name": "Mozart"
}
]
}
}
然后我們可以通過(guò)以下方式獲取值:
MetaData
獲取最外層值:
let type = request.data["metadata"].string // "some metadata"
Items
獲取嵌套值:
let items = request.data["artists", "items"] // [["name": "Van Gogh"], ["name": "Mozart"]]
Mixing Arrays and Objects
獲取第一個(gè)“artists”:
let first = request.data["artists", "items", 0] // ["name": "Van Gogh"]
Array Item
從item獲取key:
let firstName = request.data["artists", "items", 0, "name"] // "Van Gogh"
Array Comprehension
遍歷數(shù)組检盼,只取artists 的name:
let names = request.data["artists", "items", "name"] // ["Van Gogh", "Mozart"]