我正面临AFNetworking 2.0(在Swift上编码)的问题。库的GET方法似乎对我来说非常适合。虽然,POST方法返回错误。错误是错误的用户名/密码。从第一次看起来似乎是一个身份验证问题,但实际上当我在Postman(REST Client)上尝试POST请求时工作得很完美。
所以我在做什么:
配置:
var policy : AFSecurityPolicy = AFSecurityPolicy();
policy.allowInvalidCertificates = true;
self.client = AFHTTPRequestOperationManager(baseURL: self.basicUrl)
self.client!.operationQueue = NSOperationQueue.mainQueue()
self.client!.securityPolicy = policy
var responseSerializer : AFJSONResponseSerializer = AFJSONResponseSerializer()
self.client!.responseSerializer = responseSerializer
var requestSerializer : AFJSONRequestSerializer = AFJSONRequestSerializer()
requestSerializer.setValue("application/json", forHTTPHeaderField: "Accept")
self.client!.requestSerializer = requestSerializer
和我的要求
internal func loginUser(email : String, password : String, onCompletion : WebClientLoginResponse) -> Void
{
var params : Dictionary = ["email":email, "pass":password]
self.client!.POST("login", parameters: params, success: { (operation : AFHTTPRequestOperation!, response : AnyObject!) -> Void in
var finalResponse : Dictionary = Dictionary<String, String>()
finalResponse = response as Dictionary
onCompletion(true, nil, nil, nil)
}) { (operation : AFHTTPRequestOperation!, error : NSError!) -> Void in
println("Error \(error.description) \(operation.responseObject) ")
onCompletion(false, nil, nil, nil)
}
}
你觉得伙计们怎么样?我错过了什么吗?
谢谢!
答案 0 :(得分:2)
您的服务器是否需要JSON编码的参数?请参阅已接受的answer to this question。
另外,请考虑使用Swift版本的AFNetworking AlamoFire,它有一种更简单(IMHO)的方式来设置编码格式。