I'm having trouble getting an image from S3 using a presigned url and AFNetworking 3.0
. I can get the image using NSMutableURLRequest
and an NSURLSession
, but I get a 403 error when I use an AFHTTPSessionManager
and the same NSMutableURLRequest
. (I actually want to load the image by just calling setImageWithURLRequest, which is also giving me a 403 error, but I wrote this code that uses AFHTTPSessionManager
to try to debug the issue.) What could I be doing wrong with AFNetworking
?
let request = NSMutableURLRequest()
request.HTTPMethod = "GET"
request.URL = coverPhotoUrl // A presigned url to an image on S3
// The operation succeeds if I uncomment the NSURLSession line
// and comment out the AFHTTPSessionManager line.
//NSURLSession.sharedSession().dataTaskWithRequest(request) {
AFHTTPSessionManager().dataTaskWithRequest(request) {
date, response, error in
if let error = error {
Log.error?.message("request failed: \(error.localizedDescription)")
} else {
Log.debug?.message("request succeeded.")
}
}
.resume()
AFNetworking was failing the request due to content type issues. setImageWithURL worked after I changed the S3 files' content type from binary/octet to image/jpeg.
Getting S3 to set the content type was also difficult. In case it's of use to anyone else, S3 ignored the content type in my PUT requests until I specified a content length. I just used a dummy length of 1 when generating the presigned URL despite the fact that the actual files are far longer.