title: Multipart/form-data POST request Swift
body: <p>My code is executing the HTTP POST request correctly to my PHP endpoint. However, all the data I need to send over is not arriving. This is the code I have for sending the image and other parameters.The problem that I am having is that the PHP code does not recognize the key value pairs (i.e. $_POST['DATE']). In my PHP code, I echo back all the POST dictionary values and they come out empty.</p>
  
  <pre><code>var imageData : NSData = UIImageJPEGRepresentation(Record.sharedInstance.picture, 1.0)
  
      let url = NSURL(string:"URL")
      let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
      var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
  
      request.HTTPMethod = "POST"
  
      let boundary    = "Boundary-\(NSUUID().UUIDString)"
      let contentType = "mulitpart/form-data; boundary=\(boundary)"
  
      request.setValue(contentType, forHTTPHeaderField: "Content-Type")
  
      var body = NSMutableData()
  
      var tempData      = NSMutableData()
      let fileName      = "sharkImage" + ".jpg"
      let parameterName = "PHOTOGRAPH"
  
      let mimeType = "image/jpeg"
  
      let fileNameContentDisposition = String(fileName) != nil ? "filename=\"\(fileName)\"" : ""
      let contentDisposition = "Content-Disposition: form-data; name=\"\(parameterName)\";\(fileNameContentDisposition)\r\n"
  
      println(Record.sharedInstance.mImagePath)
  
  
      tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
      tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
      tempData.appendData("Content-Type : \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
      tempData.appendData(imageData)
      tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
  
      body.appendData(tempData)
      body.appendData(buildBody("DATE", boundary: boundary, value: Record.sharedInstance.mDate))
      body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!)
      request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
      request.HTTPBody = body
  
      let task = NSURLSession.sharedSession().dataTaskWithRequest(request, completionHandler: { data, response, error in
  
      if error != nil {
          return
      }
  
      // if response was JSON, then parse it
      var parseError: NSError?
      let responseObject: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: &amp;parseError)
          if let responseDictionary = responseObject as? NSDictionary {                
              let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
  
          println("responseString = \(responseString)")
  
      } else { // if not JSON
              let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
              println("responseString = \(responseString)")
      }
  
      })
  
      task.resume()
  </code></pre>