Mvj
Mvj

Reputation: 201

PUT NSData to PreSigned URL from S3 with Alamofire.upload(...) not working on iOS 8

The following code works fine on iOS9 but when run on iOS8 it fails and Amazon is returning error 400. The response only contain headers:

The NSData is an image and the Content-Type is "image/png", which tells amazon not to store it as a "binary/octet-stream".

func uploadFile(locationURL: String, http: Alamofire.Method, mimeType: String, fileData: NSData) -> ApiCaller {
    Alamofire.upload(http, locationURL, headers: ["Content-Type": mimeType], data: fileData)
        .progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
            if let uploadProgress = self.uploadProgress {
                uploadProgress(bytesWritten, totalBytesWritten, totalBytesWritten);
            }
        }
        .response { (req, res, json, error) in
            self.returnResult(req, res: res, json: json, error: error, tag: 0)
            return();
        }
    return self;
}

Upvotes: 2

Views: 2161

Answers (1)

Mvj
Mvj

Reputation: 201

This guy guided me to the answer: Other guy having similar issue.

It turns out that the HTTPAdditionalHeaders on the manager session in Alamofire had headers from my previous calls and Amazon S3 didn't like this on iOS 8.

Therefore all I needed, was to clear the headers before using the .upload(...) function.

Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = [:];

Upvotes: 1

Related Questions