title: How to escape the HTTP params in Swift
body: <p>I'm trying to make a post to an HTTP server </p>
  
  <p>By the way this is my code:</p>
  
  <pre><code>  func sendPostToUrl(url:String, withParams params: [String: String?] ) {
      var request = NSMutableURLRequest(URL: NSURL(string: url)!)
      var session = NSURLSession.sharedSession()
      request.HTTPMethod = "POST"
  
      var err: NSError?
      var bodyData = ""
      for (key,value) in params{
          if (value==nil){ continue }
          let scapedKey = key.stringByAddingPercentEncodingWithAllowedCharacters(
                   .URLHostAllowedCharacterSet())!
          let scapedValue = value!.stringByAddingPercentEncodingWithAllowedCharacters(
                  .URLHostAllowedCharacterSet())!
          bodyData += "\(scapedKey)=\(scapedValue)&amp;"
      }
      request.HTTPBody = bodyData.dataUsingEncoding
                 (NSUTF8StringEncoding, allowLossyConversion: true)
  
      var task = session.dataTaskWithRequest(request, 
      completionHandler: {data, response, error -&gt; Void in
          println("Response: \(response)")
          let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
          println("Data: \(dataString)")
  
      })
      task.resume()
  }
  </code></pre>
  
  <p>It works but is not perfect. If I call the function this way:</p>
  
  <pre><code>    client.sendPostToUrl("http://novagecko.com/tests/test.php", 
          withParams: ["hello":"world","inject":"param1=value1&amp;param2=value2"]);
  </code></pre>
  
  <p>The server detects 3 post fields (with keys <code>hello</code>,<code>inject</code> and <code>param2</code>) instead of 2.</p>
  
  <p>How can I escape the key and values?</p>
  
  <p>Is there something more I could do for improving the method?</p>