Alamofire仅在GET请求上请求错误 [英] Alamofire Request error only on GET requests
问题描述
我正在努力将我的项目从AFNetworking转移到Alamofire。真的很喜欢这个项目。 POST请求工作正常,但是,我在尝试发出GET请求时收到此错误。
I'm working on transferring my project from AFNetworking to Alamofire. Really like the project. POST requests work just fine, however, I'm receiving this error when attempting to make a GET request.
以下是一些示例代码:
class func listCloudCredntials(onlyNew onlyNew: Bool = true, includePending: Bool = true) -> Request {
let parameters: [String: AnyObject] = includePending ? ["include_pending": "true"] : [:]
let urlString = "https://myapp-staging.herokuapp.com/api/1/credntials"
let token = SSKeychain.storedToken()
let headers: [String: String] = ["Authorization": "Bearer \(token)"]
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .JSON, headers: headers)
}
我收到此错误:: -1005网络连接丢失
但是,如果我将请求类型更改为 .POST
,请求正常。我收到401代码,但至少请求不会丢失网络连接。
However, if I change the request type to .POST
, the request "works". I receive a 401 code, but at least the request doesn't lose Network connection.
我做错了什么?
推荐答案
您将参数编码为请求正文中的JSON,尝试通过将编码更改为 URL 来编码URL中的参数
:
You're encoding the parameters as JSON in the body of the request, try encoding the parameters in the URL by changing the encoding to URL
:
return Alamofire.request(.GET, urlString, parameters: parameters, encoding: .URL, headers: headers)
由于这是默认行为,您只需删除它:
As this is the default behavior, you can simply remove it:
return Alamofire.request(.GET, urlString, parameters: parameters, headers: headers)
这篇关于Alamofire仅在GET请求上请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!