4

I try to download images from Amazon S3 server via AlamofireImage framework.

The Images on the S3 server, save with 'Content-Type' = 'binary/octet-stream'.

In the beginning I got the Error:

Failed to validate response due to unacceptable content type.

So, I tried to change/update the HTTP Header's request in order to support with binary/octet-stream'

I updated the method:

private func URLRequestWithURL(URL: NSURL) -> NSURLRequest 

In the UIImageView+AlamofireImage.swift file to:

private func URLRequestWithURL(URL: NSURL) -> NSURLRequest {
    let mutableURLRequest = NSMutableURLRequest(URL: URL)
    mutableURLRequest.addValue("binary/octet-stream", forHTTPHeaderField: "Content-Type")
    return mutableURLRequest
}

And is still not working, Just after I added the:

let contentTypes: Set<String> = ["Content-Type", "binary/octet-stream"]
Request.addAcceptableImageContentTypes(contentTypes)

The problem was solved, But I really don't like the fact that I changed a private method in the AlamofireImage framework.

I wonder if there is an elegant way to solve this problem, given I can't change the images 'Content-Type' in the S3 server.

Thanks

Guy Kahlon
  • 4,510
  • 4
  • 30
  • 41

2 Answers2

3

Doing Request.addAcceptableImageContentTypes(["binary/octet-stream"]) should be all that you need to get it to work.

If you were using af_setImageWithURL, there was a bug that it wasn't using the acceptableImageContentTypes. AlamofireImage 2.2.0 fixes that.

brussell
  • 111
  • 2
3

In Swift 3.2 it's slightly different.

       let request = URLRequest(url: URL)
        DataRequest.addAcceptableImageContentTypes(["binary/octet-stream"])
        AlamoDownloader.shared.imageDownloader.download(request){ response in

DataRequest is an Alamofire public class.

SafeFastExpressive
  • 3,637
  • 2
  • 32
  • 39