title: How to make NSURLSession POST request in Swift
body: <p>Hi I am very beginner for Swift and I am trying to make NSURLSession "Post" request sending some parameter like my below code </p>
  
  <p>According to my below code response not coming from server can some one help me please</p>
  
  <h2>BackGroundClass:-</h2>
  
  <pre><code> import UIKit
  
  protocol sampleProtocal{
  
      func getResponse(result:NSDictionary)
      func getErrorResponse(error:NSString)
  }
  
  class BackGroundClass: NSObject {
  
      var delegate:sampleProtocal?
  
      func callPostService(url:String,parameters:NSDictionary){
  
  
          print("url is===&gt;\(url)")
  
          let request = NSMutableURLRequest(URL: NSURL(string:url)!)
  
          let session = NSURLSession.sharedSession()
          request.HTTPMethod = "POST"
  
          //Note : Add the corresponding "Content-Type" and "Accept" header. In this example I had used the application/json.
          request.addValue("application/json", forHTTPHeaderField: "Content-Type")
          request.addValue("application/json", forHTTPHeaderField: "Accept")
  
          request.HTTPBody = try! NSJSONSerialization.dataWithJSONObject(parameters, options: [])
  
          let task = session.dataTaskWithRequest(request) { data, response, error in
              guard data != nil else {
                  print("no data found: \(error)")
                  return
              }
  
              do {
                  if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                      print("Response: \(json)")
                      self.mainResponse(json)
                  } else {
                      let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)// No error thrown, but not NSDictionary
                      print("Error could not parse JSON: \(jsonStr)")
                      self.eroorResponse(jsonStr!)
                  }
              } catch let parseError {
                  print(parseError)// Log the error thrown by `JSONObjectWithData`
                  let jsonStr = NSString(data: data!, encoding: NSUTF8StringEncoding)
                  print("Error could not parse JSON: '\(jsonStr)'")
                  self.eroorResponse(jsonStr!)
              }
          }
  
          task.resume()
      }
  
      func mainResponse(result:NSDictionary){
          delegate?.getResponse(result)
      }
  
      func eroorResponse(result:NSString){
          delegate?.getErrorResponse(result)
      }
  }
  </code></pre>
  
  <h2>ViewController:-</h2>
  
  <pre><code>import UIKit
  
  class ViewController: UIViewController,sampleProtocal {
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          let delegate = BackGroundClass();
          delegate.self;
  
          let params = ["scancode":"KK03799-008", "UserName":"admin"] as Dictionary&lt;String, String&gt;
  
          let backGround=BackGroundClass();
          backGround.callPostService("url", parameters: params)
      }
  
      func getResponse(result: NSDictionary) {
          print("Final response is\(result)");
      }
  
      func getErrorResponse(error: NSString) {
          print("Final Eroor code is\(error)")
      }
  }
  </code></pre>