Maxim Tsybanov Maxim Tsybanov - 9 months ago 53
Swift Question

How to send POST parameters in Swift?

Here some code:

var URL: NSURL = NSURL(string: "")
var request:NSMutableURLRequest = NSMutableURLRequest(URL:URL)
request.HTTPMethod = "POST"
request.HTTPBody = ??????
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue())
(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))

What should I write in
if I want send POST query
"key" = "value"


No different than in Objective-C, HTTPBody expects an NSData object:

var bodyData = "key1=value&key2=value&key3=value"
request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding);

You'll have to setup the values & keys yourself in the string.