swift中的POST请求

时间:2015-02-02 22:51:30

标签: api swift post nsurlsession mashape

好的,我看起来高低不能找到答案。所以我只想尝试从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]&nbsp;=&nbsp;10<br>a[1]&nbsp;=&nbsp;11<br>a[2]&nbsp;=&nbsp;12<br>a[3]&nbsp;=&nbsp;13<br>a[4]&nbsp;=&nbsp;14<br>a[5]&nbsp;=&nbsp;15<br>a[6]&nbsp;=&nbsp;16<br>a[7]&nbsp;=&nbsp;17<br>a[8]&nbsp;=&nbsp;18<br>a[9]&nbsp;=&nbsp;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但我把它包含在params字典中?任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

好的,任何人都有同样的问题我将发布解决方案。因此@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的信息。