title: Multipart-form data POST to upload image not working after boundary is added in Swift
body: <p>So I'm trying to do a multi-part post request in Swift using the following format: </p>
  
  <pre><code>user_id 3232424234
  photo *PHOTO DATA*
  </code></pre>
  
  <p>I set up my request as shown at the bottom of this post and am getting the following error: </p>
  
  <blockquote>
    <p>Optional(Error Domain=NSURLErrorDomain Code=-1005 "The network connection        was lost." UserInfo={NSUnderlyingError=0x7f86235cfe40 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=<strong>ENDPOINTURL</strong>, NSErrorFailingURLKey=<strong>ENDPOINTURL</strong>, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.})</p>
  </blockquote>
  
  <p>This error seems extremely odd to me because I can make it go away by omitting the boundary requirements for the POST request, but the server than explodes with a 500 and lets the client know it omitted the boundary reqs, so yah. I am probably doing something wrong in Swift. Below is my code to make the request. Let me know if more info is needed, thx guru's of the world. </p>
  
  <pre><code>//Inputs in this scope are an "id" and a "photo"
  let url = NSURL(string: "**URL**")
  let boundary = NSUUID()
  let request = NSMutableURLRequest(URL: url!)
  
  request.HTTPMethod = "POST"
  request.setValue("multipart/form-data; boundary=--\(boundary)", forHTTPHeaderField: "Content-Type")
  
  let parameterBody:NSDictionary = [
      "user_id": id,
  ]
  
  let data:NSMutableData = NSMutableData()
  
  parameterBody.enumerateKeysAndObjectsUsingBlock { (parameterKey: AnyObject, parameterValue: AnyObject, stop: UnsafeMutablePointer&lt;ObjCBool&gt;) -&gt; Void in
      data.appendData(NSString(string: "--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
      data.appendData(NSString(string: "\(parameterKey) \(parameterValue)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
  }
  
  data.appendData(NSString(string: "--\(boundary)\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
  data.appendData(NSString(string: "photo").dataUsingEncoding(NSUTF8StringEncoding)!)
  data.appendData(photo.imageData as! NSData)
  data.appendData(NSString(string: "--\(boundary)--\r\n").dataUsingEncoding(NSUTF8StringEncoding)!)
  
  request.setValue("\(data.length)", forHTTPHeaderField: "Content-Length")
  
  let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: data) { (responseJSON: AnyObject?, response: NSURLResponse?, error: NSError?) -&gt; Void in
      //Here I handle the response and check for errors.
  }
  
  task.resume()
  </code></pre>