紀錄一下等同於Objective C神級AFNetworking的Alamofire
request&response都為JSON format時,GET request使用方式如下範例
let url = "你的API網址"
let parameters: Parameters = ["CompanyId": "AAAA"]
let headers: HTTPHeaders = [
"客製的header": "BBBB",
"Accept": "application/json"
]
Alamofire.request(url, method: HTTPMethod.get, parameters: parameters, encoding:URLEncoding.default , headers: headers).responseJSON { response in
if let error = response.result.error {
print("GetCompanyInformation error: \(String(describing: error))")
} else {
print("Request: \(String(describing: response.request))") // original url request(原始的URL要求)
print("Response: \(String(describing: response.response))") // http url response(URL回應)
print("Result: \(response.result)") // response serialization result(回應的序列化結果)
if let json = response.result.value {
print("JSON: \(json)") // serialized json response
}
if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
print("Data: \(utf8Text)") // original server data as UTF8 string(伺服器資料)
}
}
}
沒有留言:
張貼留言