Here is a Swift version. Note that if you do not want to send form data it is still important to send the empty form boundary. Flask in particular expects form data followed by file data and will not populate request.files
without the first boundary.
let composedData = NSMutableData()
// Set content type header
let BoundaryConstant = "--------------------------3d74a90a3bfb8696"
let contentType = "multipart/form-data; boundary=\(BoundaryConstant)"
request.setValue(contentType, forHTTPHeaderField: "Content-Type")
// Empty form boundary
composedData.appendData("--\(BoundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
// Build multipart form to send image
composedData.appendData("--\(BoundaryConstant)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
composedData.appendData("Content-Disposition: form-data; name=\"file\"; filename=\"image.jpg\"\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
composedData.appendData("Content-Type: image/jpeg\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
composedData.appendData(rawData!)
composedData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
composedData.appendData("--\(BoundaryConstant)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
request.HTTPBody = composedData
// Get content length
let length = "\(composedData.length)"
request.setValue(length, forHTTPHeaderField: "Content-Length")