Search code examples
swiftswiftuialamofirecombine

Swift upload UIImage with multipart form without parameters


Server I work with accepts:

  curl -X 'POST' \
  'https://some.url' \
  -H 'accept: application/json' \
  -H 'authorization: Bearer YOUR_TOKEN' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=image/jpeg'

All I can find is examples with parameters. I have tried everything, alamofire and standard url sessions


Solution

  • For some reason only Alamofire worked this is final method I've used.

    AF.upload(
                  multipartFormData: { multipartFormData in
                      multipartFormData.append(image.jpegData(compressionQuality: 0.8)!, withName: UUID().uuidString, fileName: "file.jpeg", mimeType: "image/jpg")
              },
                  to: "https://someurl.com", method: .post , headers: headers)
                .uploadProgress(queue: .main, closure: { progress in
                    //Current upload progress of file
                    print("Upload Progress: \(progress.fractionCompleted)")
                        })
                .responseJSON(completionHandler: { data in
                    //Do what ever you want to do with response
                    print(data)
                        })