最基本的GET請求
Alamofire.request("xxx")
我們也可以直接獲得json數(shù)據(jù)
簡單了解一下JSON http://m.itdecent.cn/p/30873633912a
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print("Request: \(String(describing: response.request))") // 最初的 url request
print("Response: \(String(describing: response.response))") // http url response
print("Result: \(response.result)") // 序列化 result
if let json = response.result.value {
print("JSON: \(json)") // 打印json
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string
}
}
這個被各種教程推薦的庫 支持多種返回序列化數(shù)據(jù)的方法
response()
responseData()
responseString(encoding: NSStringEncoding)
responseJSON(options: NSJSONReadingOptions)
responsePropertyList(options: NSPropertyListReadOptions)
請求時的一些細(xì)節(jié)
方法默認(rèn)為 .get
Alamofire.request("https://httpbin.org/post", method: .post)
Alamofire.request("https://httpbin.org/put", method: .put)
Alamofire.request("https://httpbin.org/delete", method: .delete)
GET Request With URL-Encoded Parameters
let parameters: Parameters = ["foo": "bar"]
// All three of these calls are equivalent
Alamofire.request("https://httpbin.org/get", parameters: parameters) // encoding defaults to `URLEncoding.default`
Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding.default)
Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding(destination: .methodDependent))
// https://httpbin.org/get?foo=bar