大家下午好,新手在這裏試圖學習Swift/Alamofire。我已經掙扎了近兩天,並準備繼續前進。Alamofire編碼invalidURL
我的URL看起來已經被創建了,但是在提出請求時出現錯誤。在查看Xcode調試日誌時,看起來好像我的單引號在請求發送時正在被轉義。
let owner : String = "[email protected]"
func getDeploymentsByOwner(token: String, owner: String, success: @escaping (JSON) -> Void, failure: @escaping (Error) -> Void) {
let headers = [
"Authorization": "Bearer \(token)",
]
print(owner)
let url = "https://corp.local?$filter=owners/ref eq '\(owner)'"
// created URL is https://corp.local?$filter=owners/ref eq '[email protected]'
Alamofire.request(url, method: .get, encoding: URLEncoding.queryString, headers: headers).responseJSON { (responseObject) -> Void in
if responseObject.result.isSuccess {
let resJSON = JSON(responseObject.result.value!)
success(resJSON)
}
if responseObject.result.isFailure {
let error = responseObject.result.error
print(error)
failure(error!)
}
}
我的錯誤消息看起來像這樣 // Xocde調試錯誤
Optional(Alamofire.AFError.invalidURL("https//:corp.local?$filter=owners/ref eq \'[email protected]\'"))
invalidURL(https://corp.local?$filter=owners/ref eq \'[email protected]\'")
我能不$過濾器中的所有項目,所以只要省略了過濾器的要求工作。
//郵差的工作原理與生成的URL,但一用反斜槓()將不會
嘗試對網址進行編碼 –
感謝您的反饋。你如何編碼它? – TryingToMakeItWork
你能解決這個問題嗎? –