大家下午好,新手在这里试图学习 Swift / Alamofire.我已经挣扎了将近两天,我已准备好继续前进了. 我的网址似乎已更正,但我在发出请求时收到错误消息.在查看Xcode调试日志时,看起来我的单
我的网址似乎已更正,但我在发出请求时收到错误消息.在查看Xcode调试日志时,看起来我的单引号在发送请求时被转义.
let owner : String = "me@corp.local" 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 'me@corp.local' 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 \'me@corp.local\'")) invalidURL(https://corp.local?$filter=owners/ref eq \'me@corp.local\'")
我可以获得没有$filter的所有项目,因此只要省略过滤器,请求就会起作用.
// Postman使用生成的URL,但带有反斜杠()的URL不会
谢谢你的回复,遗憾的是它没有用.整个网址都被破坏了,包括主机名.但它确实给了我一个解决它的起点,谢谢.工作代码如下let url = "https://corp.local?$filter=owners/ref eq '\(owner)'" let encodedUrl = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)