How to upload image to awes pre signed URL as multipart data using Alamo ire 5?

I am trying to upload an image to the pre-signed AWS URL. I tried with Alamofire 5 upload method which accepts multipartData. I am getting a 403 error with Alamofire 5 multipart upload.

When I try with URLSession it is working properly.

/// Working code

        var request: URLRequest = URLRequest(url: requestURL)
        request.httpMethod = "PUT"
        request.httpBody = image
        request.setValue("image/jpeg", forHTTPHeaderField: "Content-Type")
        let tasksession: URLSessionDataTask = session.dataTask(with: request, completionHandler: { (response, urlResp, error) in
            if let data: Data = response {
                do {
                    let json: [String: Any]? = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
                    debugPrint("json \(json)")
                }
                catch {
                    debugPrint("error \(error)")
                }
            }



            print(response ?? "response nil")
            print(error ?? "response nil")
        })
        tasksession.resume()

Same when I try with AF 5 it is not working

// Not working code.

 AF.upload(multipartFormData: { (multiPart) in

            multiPart.append(imageData, withName: "file", fileName: "image.jpg", mimeType: "image/jpeg")
            }, to: url, method: .put, headers: ["Content-Type": "image/jpeg"],
                                                  ])
        .uploadProgress(queue: .main, closure: { progress in
                    //Current upload progress of the file
                    print("Upload Progress: \(progress.fractionCompleted)")
                })
                .responseJSON(completionHandler: { data in
                    let json: [String: Any]? = data as? [String: Any]
                    debugPrint("upload complete json \(data)")
            })

Could someone tell why it is not working with multipart upload with Alamofire 5, multipart upload is working in Android.

This Post Has One Comment

  1. No Fault

    Alamo ire Version 4.9.1

    Alamofire.upload(multipartFormData: { (multipartFormData) in
    multipartFormData.append(imageData, withName: “file”, fileName: “image.jpg”, mimeType: “image/jpeg”)
    //Any Post Params if you have.
    for (key, value) in parameters {
    multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
    }
    }, to:uploadUrlStr) //uploadUrlStr: upload url in your case
    { (result) in
    switch result {
    case .success(let upload, _, _):
    upload.uploadProgress(closure: { (progress) in
    print(“Uploading”)
    print(CGFloat(progress.fractionCompleted * 100))
    })

    upload.responseJSON { response in
    print(“Upload Finished”)
    guard let resultValue = response.result.value else {
    NSLog(“Result value in response is nil”)
    return
    }

    }

    case .failure(let encodingError):
    print(encodingError.localizedDescription)
    }
    }
    This is working for me.

Leave a Reply