0
votes

I have a working project that uses Alamofire to handle all requests including an upload with multipart data. This works fine at the moment, however we'd like to move to using GZIP for all requests and responses. I am using the gzip swift library and can now receive gzipped data through requests, but I can't figure out how to gzip encode the multipart data which is causing the call to fail.

I have searched for a solution and although I have come across an article regarding it, the examples provided were for an older Alamofire and Swift build. I tried to hack around with it to get it working, but there were a few errors which I could not resolve.

My request code is as follows:

            self.sessionManager.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append("\(sUUID)".data(using: String.Encoding.utf8)!, withName: "sUUID")
                multipartFormData.append(sStream, withName: "files[]", fileName: fileName, mimeType: "image/\(sExt)")
                multipartFormData.append("\(convertedEventID)".data(using: String.Encoding.utf8)!, withName: "nEventID")
                multipartFormData.append("\(sExt)".data(using: String.Encoding.utf8)!, withName: "sExt")
                multipartFormData.append("0".data(using: String.Encoding.utf8)!, withName: "bRecurring")
                multipartFormData.append("\(fileName)".data(using: String.Encoding.utf8)!, withName: "sDescription")
                multipartFormData.append("File Attached By User".data(using: String.Encoding.utf8)!, withName: "sWordDocumentType")
                multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sWordDocumentName")
                multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sSname")
                multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sEmailRecip")
                multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sEmailSender")
                multipartFormData.append("".data(using: String.Encoding.utf8)!, withName: "sNotes")
                multipartFormData.append("ME".data(using: String.Encoding.utf8)!, withName: "sAgent")
        },
            to: "/Upload",
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseString { response in
                        let decompressedData: Data
                        if response.data!.isGzipped {
                            decompressedData = try! response.data!.gunzipped()
                        } else {
                            decompressedData = response.data!
                        }
                        NotificationCenter.default.post(name: .didReceiveData, object: self, userInfo: ["pass" : 1])
                        completion(true, 200, decompressedData, nil)
                    }
                    upload.uploadProgress { progress in
                        print(progress.fractionCompleted)
                    }
                case .failure(let encodingError):
                    print(encodingError)
                    completion(false, 404, nil, encodingError)
                }
        })
1

1 Answers

0
votes

You won't be able to use the built in upload(multipartFormData:...) method if you need to do post processing like gzip encoding. Instead, you'll need to use Alamofire's multipart encoding separately, generate the data, gzip it, then upload it using Alamofire.

Also, you won't need to manually unzip the response. As long as the appropriate Content-Encoding header comes back indicating that it has be gzipped, it should be uncompressed for you automatically.

Finally, you can more easily create Data values from Strings by using Data("string".utf8).