好吧,我看了高看低,找不到答案。所以我只是想从 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] = 10na[1] = 11na[2] = 12na[3] = 13na[4] = 14na[5] = 15na[6] = 16na[7] = 17na[8] = 18na[9] = 19n", "async": 0, "memory_used": "64"}, "compile_status": "OK", "web_link": "http://blah.com/cc6581D"})
它说需要client_secret,但我将其包含在参数字典中?任何帮助将不胜感激!
好的,
如果有人遇到同样的问题,我将发布解决方案。因此,正如@findall指出的那样,正确的编码不是JSON,它只是应该是一个URL编码。但是在我将其更改为 url 编码后,它仍然不起作用。所以我给了阿拉莫火一个镜头,它完全奏效了!这是代码:
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的事情。