1
我想使用JSON參數和自定義標頭使用Alamofire與Swift 3和XCode 8做一個HTTP GET請求。我已經能夠成功地使用一些已經有不同的路線了,但我似乎無法破解特別的代碼。下面是我試圖做的等效CURL和我試圖用來複制它的Swift代碼。alamofire 499當獲取請求的標題和參數
捲曲請求(作品):
curl -X GET url -d '{"xxxx": "111111111" }' -v -H "email: [email protected]" -H "token: ASDFJKL" -H 'Content-Type: application/json'
Alamofire請求(不工作):
let headers: HTTPHeaders = [ "email": "[email protected]", "token": "ASDFJKL"]
let parameters: Parameters = [ "xxxx": "111111111"]
Alamofire.request(url, method: .get, parameters: parameters, encoding: JSONEncoding.default,
headers: headers).responseJSON { response in
debugPrint(response)
}
這裏是我得到回來,如果它有助於錯誤:
[Request]: <url>/<route>
[Response]: <NSHTTPURLResponse: 0x608000226fc0> { URL: <url> } { status code: 499, headers {
"Cache-Control" = "no-cache, no-store";
Connection = "keep-alive";
"Content-Length" = 435;
"Content-Type" = "text/html; charset=utf-8";
Date = "Sun, 25 Dec 2016 23:45:38 GMT";
Server = Cowboy;
} }
[Data]: 435 bytes
[Result]: FAILURE: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))
[Timeline]: Timeline: { "Request Start Time": 504402284.293, "Initial Response Time": 504402339.616, "Request Completed Time": 504402339.617, "Serialization Completed Time": 504402339.617, "Latency": 55.323 secs, "Request Duration": 55.324 secs, "Serialization Duration": 0.000 secs, "Total Duration": 55.324 secs }
這非常有幫助。非常感謝。 「curl」正在發揮作用的事實讓我很失望,所以它有助於理解爲什麼會這樣。我將在請求正文中爲GET和JSON使用URL編碼的參數。 – brownmagik352
接受。非常感謝! – brownmagik352