title: How to send form data in POST request in Swift 3
body: <p>I am trying to post form-data using webservice, userName &amp; password, but in response it's showing an error stating "Could not connect to the server.". </p>
  
  <p>Please help me to send form data in the POST request.</p>
  
  <pre><code>        let dict:[String:String] = ["userName": userName as! String, "password": password as! String]
  
          do {
  
              let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
  
              let url = URL(string: "(some url)")!
  
              var request  = URLRequest(url: url)
  
              request.httpMethod = "POST"
  
              request.setValue("application/x-www-form-urlencoded charset=utf-8", forHTTPHeaderField: "Content-Type")
  
              request.httpBody = jsonData
  
              let task = URLSession.shared.dataTask(with: request) { data, response, error in
  
                  if error != nil {
  
                      print(error!.localizedDescription)
  
                      return
  
                  }
  
                  do {
  
                      let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
  
                      if let parseJSON = json {
  
                          let resultValue:String = parseJSON["success"] as! String;
  
                          print("result: \(resultValue)")
  
                          print(parseJSON)
  
                      }
  
                  } catch let error as NSError {
  
                      print(error)
  
                  }
              }
  
              task.resume()
  
          } catch {
  
              print(error.localizedDescription)
  
          }
  </code></pre>
  
  <p>I've tried adding values in the request, may be some values are missing in the request formed. Please help!</p>
  
  <p><a href="https://i.stack.imgur.com/SsUax.png" rel="noreferrer">Thats the POSTMAN response</a></p>