POST 在 swift 中请求
POST request in swift
好吧我翻来覆去找不到答案。所以我只是想从 iPhone 应用向 mashape api 发出一个简单的 POST 请求。这是我目前所拥有的:
var headers: NSDictionary = ["X-Mashape-Key": "Hm5NokCvUamshPFfnQCRJKne3UuCp1C******************", "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"]
var params = ["async": "0", "client_secret": "ceaf93f10f7330318ae*********************", "input": "<required>", "lang": "En", "memory_limit": "262144", "source": "From the web", "time_limit": "10"] as Dictionary
var url : NSURL = NSURL(string: "https://blah.mashape.com/location/")!
var request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err) as NSData!
if err != nil {
println(err)
}
request.HTTPMethod = "POST"
request.allHTTPHeaderFields = headers
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")
})
task.resume()
但是当我收到响应时,strData 是这样说的:
Body: Optional({"errors": {}, "code_id": "cc6581D", "message": "ArgumentMissingError: client_secret is needed!", "run_status": {"status": "AC", "time_limit": 5, "output_html": "a[0] = 10<br>a[1] = 11<br>a[2] = 12<br>a[3] = 13<br>a[4] = 14<br>a[5] = 15<br>a[6] = 16<br>a[7] = 17<br>a[8] = 18<br>a[9] = 19<br>", "memory_limit": 262144, "time_used": "0.1006", "signal": "OTHER", "status_detail": "N/A", "output": "a[0] = 10\na[1] = 11\na[2] = 12\na[3] = 13\na[4] = 14\na[5] = 15\na[6] = 16\na[7] = 17\na[8] = 18\na[9] = 19\n", "async": 0, "memory_used": "64"}, "compile_status": "OK", "web_link": "http://blah.com/cc6581D"})
它说 client_secret 是必需的,但我将其包含在参数字典中?如有任何帮助,我们将不胜感激!
好的,如果有人遇到同样的问题,我将 post 解决方案。所以正如@findall 指出的那样,我正确的编码不是 JSON 而它应该是 url 编码。但在我将其更改为 url 编码后,它仍然无法正常工作。所以我给了 AlamoFire 一个机会,它完全奏效了!这是代码:
var headers: NSDictionary = ["X-Mashape-Key": "Hm5NokCvUamshPFfnQCRJKne3UuCp1Cq48FjsnMnQnEcHN0gk6", "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"]
var params = ["async": "0", "client_secret": "ceaf93f10f7330**************************", "input": "<required>", "lang": "En", "memory_limit": "262144", "source": "Link", "time_limit": "10"] as Dictionary
var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
defaultHeaders["X-Mashape-Key"] = "Hm5NokCvUamshPFfnQ********************************"
defaultHeaders["Content-Type"] = "application/x-www-form-urlencoded"
defaultHeaders["Accept"] = "application/json"
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = defaultHeaders
let manager = Alamofire.Manager(configuration: configuration)
var request = Alamofire.Manager(configuration: configuration).request(.POST, "https://blah.mashape.com/location/", parameters: params)
request.responseString({ (request, response, string, error) in
println(string)
})
谢谢@chris 告诉我有关 AlamoFire 的信息。
好吧我翻来覆去找不到答案。所以我只是想从 iPhone 应用向 mashape api 发出一个简单的 POST 请求。这是我目前所拥有的:
var headers: NSDictionary = ["X-Mashape-Key": "Hm5NokCvUamshPFfnQCRJKne3UuCp1C******************", "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"]
var params = ["async": "0", "client_secret": "ceaf93f10f7330318ae*********************", "input": "<required>", "lang": "En", "memory_limit": "262144", "source": "From the web", "time_limit": "10"] as Dictionary
var url : NSURL = NSURL(string: "https://blah.mashape.com/location/")!
var request = NSMutableURLRequest(URL: url)
var session = NSURLSession.sharedSession()
var err: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &err) as NSData!
if err != nil {
println(err)
}
request.HTTPMethod = "POST"
request.allHTTPHeaderFields = headers
var task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
println("Response: \(response)")
var strData = NSString(data: data, encoding: NSUTF8StringEncoding)
println("Body: \(strData)")
})
task.resume()
但是当我收到响应时,strData 是这样说的:
Body: Optional({"errors": {}, "code_id": "cc6581D", "message": "ArgumentMissingError: client_secret is needed!", "run_status": {"status": "AC", "time_limit": 5, "output_html": "a[0] = 10<br>a[1] = 11<br>a[2] = 12<br>a[3] = 13<br>a[4] = 14<br>a[5] = 15<br>a[6] = 16<br>a[7] = 17<br>a[8] = 18<br>a[9] = 19<br>", "memory_limit": 262144, "time_used": "0.1006", "signal": "OTHER", "status_detail": "N/A", "output": "a[0] = 10\na[1] = 11\na[2] = 12\na[3] = 13\na[4] = 14\na[5] = 15\na[6] = 16\na[7] = 17\na[8] = 18\na[9] = 19\n", "async": 0, "memory_used": "64"}, "compile_status": "OK", "web_link": "http://blah.com/cc6581D"})
它说 client_secret 是必需的,但我将其包含在参数字典中?如有任何帮助,我们将不胜感激!
好的,如果有人遇到同样的问题,我将 post 解决方案。所以正如@findall 指出的那样,我正确的编码不是 JSON 而它应该是 url 编码。但在我将其更改为 url 编码后,它仍然无法正常工作。所以我给了 AlamoFire 一个机会,它完全奏效了!这是代码:
var headers: NSDictionary = ["X-Mashape-Key": "Hm5NokCvUamshPFfnQCRJKne3UuCp1Cq48FjsnMnQnEcHN0gk6", "Content-Type": "application/x-www-form-urlencoded", "Accept": "application/json"]
var params = ["async": "0", "client_secret": "ceaf93f10f7330**************************", "input": "<required>", "lang": "En", "memory_limit": "262144", "source": "Link", "time_limit": "10"] as Dictionary
var defaultHeaders = Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders ?? [:]
defaultHeaders["X-Mashape-Key"] = "Hm5NokCvUamshPFfnQ********************************"
defaultHeaders["Content-Type"] = "application/x-www-form-urlencoded"
defaultHeaders["Accept"] = "application/json"
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.HTTPAdditionalHeaders = defaultHeaders
let manager = Alamofire.Manager(configuration: configuration)
var request = Alamofire.Manager(configuration: configuration).request(.POST, "https://blah.mashape.com/location/", parameters: params)
request.responseString({ (request, response, string, error) in
println(string)
})
谢谢@chris 告诉我有关 AlamoFire 的信息。