0
votes

I am attempting to upload an image (avatarImage) to the server as a formData parameter type using AlamoFire, but I am generating a "The data could not be read because it isn’t in the correct format" error everytime I attempt to post. Im not quite sure what I am doing wrong.

class func createTeamWithAvatar(avatarImage: Image) {
    let extendedURI = "\(RequestManager.baseURL)" + "\(RequestManager.ClickUpURI.Team.rawValue)"
    RequestManager.sharedAlamofireManager.upload(multipartFormData: {
        multipartFormData in

        //This generates an error: "The data could not be read because it isn’t in the correct format"
        if let imageData = UIImageJPEGRepresentation(avatarImage, 1) {
            multipartFormData.append(imageData, withName: "avatar", mimeType: "image/jpeg")
        }

        }, to: extendedURI,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON {
                        response in

                        if let statusCode = response.response?.statusCode,
                            let response = response.result.value as? Dictionary<String, AnyObject>,
                            let _ = response["id"] as? String
                            , statusCode == 200
                        {
                            completionHandler(response, nil)
                        } else {
                            let responseError = response.result.value as? Dictionary<String, AnyObject>

                            let errorInfo = responseError ?? ["err" : "Unexpected media uploading error" as AnyObject]
enter code here
                            let error = response.result.error ?? RequestManager.makeError(response.response?.statusCode ?? 500, userInfo: errorInfo)
                            completionHandler(nil, error as NSError?)
                        }
                    }
                default:
                    completionHandler(nil, RequestManager.makeError(500, userInfo: ["err" : "Multipart encoding failed" as AnyObject]))
                }
        })
    }

PS: I am using multipartFormData to upload b/c I need to send some other parameters as well (once I am able to upload an image).

2

2 Answers

0
votes

You should use

multipartFormData.append(paramData, withName: "paramName")

in case of string param you should convert string to data

data = string.data(using: String.Encoding.utf8)!
multipartFormData.append(data, withName: "stringParamName")

full code

   RequestManager.sharedAlamofireManager.upload(multipartFormData: {
    multipartFormData in

    //This generates an error: "The data could not be read because it isn’t in the correct format"
    if let imageData = UIImageJPEGRepresentation(avatarImage, 1) {
        multipartFormData.append(imageData, withName: "avatar", mimeType: "image/jpeg")
    }
    data = string.data(using: String.Encoding.utf8)!
    multipartFormData.append(data, withName: "stringParamName")

   //..... 
0
votes

I had to add .httpAdditionalHeaders b/c my server was expecting certain data:

RequestManager.sharedAlamofireManager.session.configuration.httpAdditionalHeaders = ["Accept": "application/json", "Content-Type": "multipart/form-data"]

You can add more configurations by appending a key/value pair to this array.