Swift IOS: how to make a REST POST request
I am trying to implement POST
with raw text in path
, I tried using NSMutableURLRequest
and specifying the following
request.HTTPMethod = "POST"
request.HTTPBody = "some strings here"
I was not able to get much more than this as I failed unsuccessfully in implementation session.uploadTaskWithRequest
.
This, however, is what I worked fine for the request GET
;
private func get(path: String)
{
let url = NSURL(string: path)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
println("JSON recieved")
if(error)
{
println(error.localizedDescription)
}
println("Parsing JSON")
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
if(err != nil)
{
println("Json error");
}
println("Building Array result list from JSON")
var results = jsonResult["results"] as NSArray
self.delegate?.didReceiveAPIResult(jsonResult)
println("Done with JSON response")
})
task.resume()
}
+3
source to share