【发布时间】:2016-05-03 12:49:08
【问题描述】:
我使用 Alamofire 和自定义方法将NSURLMutableRequest 发布到 WebAPI,例如:
阿拉莫火
Alamofire.request(.POST, "http://www.webapi.com?path=Login", parameters: ["username": "myname", "password": "mypass"]).responseJSON { response in
// do something with returned data ...
}
自定义
let request = NSMutableURLRequest(URL: NSURL(string:"http://www.webapi.com?path=Login")!)
let bodyData = "username=myname&password=mypass"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration())
session.dataTaskWithRequest(request) { (data, response, error) -> Void in
// do something with returned data...
}.resume()
我通过 Alamofire 方法得到了正确的结果,但不是我自己的方法。
与 Alamofire 发布方法相比,我做错了什么?
【问题讨论】:
-
你可以打开 Alamofire.request 方法。 alamofire 代码是开源的。
-
当你说你得到了正确的结果; NSURLRequest 的错误结果是什么?是崩溃、任务返回的错误、错误的数据吗?
-
@keithbhunter 正确的结果是 json 格式的数据,错误的结果只是
nil
标签: ios swift http post alamofire