title: Unable to encode UIImage into data for POST request
body: <p>I'm converting a UIImage into data to be sent to a server via a http POST request. While preparing the body for the request, the function <code>createBody</code> is called, and the image data that is passed to this function is <code>UIImageJPEGRepresentation(image, 0.7)!</code></p>
  
  <pre><code>static func createBody(parameters: [String: String],
                  boundary: String,
                  data: Data,
                  mimeType: String,
                  filename: String) -&gt; Data {
  
      let body = NSMutableData()
  
      let boundaryPrefix = "--\(boundary)\r\n"
  
      for (key, value) in parameters {
  
          body.appendString(boundaryPrefix)
          body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
          body.appendString("\(value)\r\n")
      }
  
      body.appendString(boundaryPrefix)
      body.appendString("Content-Disposition: form-data; name=\"file\"; filename=\"\(filename)\"\r\n")
      body.appendString("Content-Type: \(mimeType)\r\n\r\n")
      body.append(data) // theres something about appending this image data that is causing problems
  
      body.appendString("\r\n")
      body.appendString("--".appending(boundary.appending("--")))
  
      print(body as Data)
      print(String(data: data, encoding: .utf8) ?? "unable to print data")
  
      return body as Data
  }
  </code></pre>
  
  <p>The console reads:</p>
  
  <pre><code>368185 bytes
  unable to print data
  </code></pre>
  
  <p>When the image data is not appended to the body, <code>body</code> is printed without any issues. When the image data is appended, it is clearly there, since the size of <code>body</code> is considerably greater (368185 bytes vs 191 bytes), yet the next line reads <code>unable to print data</code>. What is it about this UIImage data that is not allowing it to be encoded along with the other text in the body? If it's to do with the encoding, then how can I have the body printed with both the text and data and how should it be formatted to be sent to a server via POST?</p>