Maxim Tsybanov Maxim Tsybanov - 1 year ago 122
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"

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download