Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Alamofire and multipart/form-data

I'm unable to approach the API that has been offered to me in the proper way for it to give me the response I'm looking for. I've been using Swift and Alamofire for a while but this is the first time I've to upload images using multipart/form-data. I'm able to upload images using Postman but I'm unable to get the same message send out by my application using the Alamofire framework.

My Postman screenshot

My Swift code:

func postFulfilWish(wish_id: Int, picture : UIImage, completionHandler: ((AnyObject?, ErrorType?) -> Void)) {

    var urlPostFulfilWish = Constant.apiUrl;
    urlPostFulfilWish += "/wishes/";
    urlPostFulfilWish += String(wish_id);
    urlPostFulfilWish += "/fulfill/images"  ;

    let image : NSData = UIImagePNGRepresentation(UIImage(named: "location.png")!)!

    Alamofire.upload(.POST, urlPostFulfilWish, headers: Constant.headers, multipartFormData: { multipartFormData in
        multipartFormData.appendBodyPart(data: image, name: "file")
        },
        encodingCompletion: { encodingResult in
            switch encodingResult {
            case .Success(let upload, _, _):
                upload.responseJSON { response in
                    //This is where the code ends up now
                    //So it's able to encode my message into multipart/form-data but it's not doing it in the correct way for the API to handle it
                    debugPrint(response)
                }
            case .Failure(let encodingError):
                print(encodingError)
            }
        }
    )
}
like image 775
Rutger Huijsmans Avatar asked Dec 25 '22 07:12

Rutger Huijsmans


2 Answers

In case it is not already answered, recently I had the same problem using Alamofire to upload an Image using form-data.

I was able to upload the image using Postman exactly as it's shown in this post, but no able to do it using Alamofire in my app.

You need to check two things, first of all, the name of the file that the server is expecting, and second the method used to append the body part in the multipartFormData closure.

This two methods were not working in my case -

multipartFormData.appendBodyPart(data: imageData, name: "file")

this one neither

multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: name)

But on this one worked splendid -

multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "file.jpeg", mimeType: "image/jpeg")

The problem basically is that the server can't find the file with the expected name.

I hope this help someone to save time wondering why it's not working.

like image 95
Esteban Vallejo Avatar answered Jan 05 '23 09:01

Esteban Vallejo


You are doing debugPrint(response). You presumably should do another switch response.result { ... } and see if you got .Success or .Failure as the result of the request, and if success, you'd look at the response object contents (or if failure, look at the failure error). You need to look at that result to diagnose whether it was successful or not.

Alamofire.upload(.POST, urlPostFulfilWish, headers: Constant.headers, multipartFormData: { multipartFormData in
    multipartFormData.appendBodyPart(data: image, name: "file")
}) { encodingResult in
    switch encodingResult {
    case .Success(let upload, _, _):
        upload.responseJSON { response in
            switch response.result {
            case .Success(let value):
                print(value)
            case .Failure(let error):
                print(error)
            }
        }
    case .Failure(let encodingError):
        print(encodingError)
    }
}
like image 37
Rob Avatar answered Jan 05 '23 09:01

Rob