Error fetch image from storage with message 'Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType'

1.2k views Asked by At

I attemp to fetch image from Firebase storage, but have error message:

Alamofire.AFError.responseValidationFailed(reason: Alamofire.AFError.ResponseValidationFailureReason.unacceptableContentType(acceptableContentTypes: ["image/x-xbitmap", "image/jpeg", "application/octet-stream", "image/gif", "image/ico", "image/tiff", "image/x-icon", "image/bmp", "image/x-bmp", "image/x-win-bitmap", "image/png", "image/x-ms-bmp"], responseContentType: "application/json")

My url looks like this: https://firebasestorage.googleapis.com/v0/b/####/o/executors/o4uAQa158nXTvJ5omuxqcRb0e793/products/7DDCEEAC-ED54-4910-B93D-5E40BF411B80

I can download this image via browser.

My image has MIME-type 'image/jpeg':enter image description here

I found the same situation:

  1. Response Content-Type was missing and acceptable content types
  2. Make sure you set alamofire acceptable content types
  3. Image Response Serializers

These hints didn't help me to fix my bug.

Version of pod:

  • Alamofire: 5.0.2
  • AlamofireImage 4.0.2

Initially I used AlamofireImage: productImageView.af.setImage(withURL: url), but it didn't work. Then I began using Alamofire. And into request I pass MIME-type image/jpeg like Content-Type: enter image description here

And I decided to use this approach to fix bug, but has the same error and I don't understand why(from docs): enter image description here

If you can see in error-message I have:

responseContentType: "application/json"

So does it have any side effect to fetch image? What I do wrong at all?

2

There are 2 answers

0
Tarik On BEST ANSWER

Link should be

https://firebasestorage.googleapis.com/v0/b/projectname/o/image.png?alt=media&token=auth_token

replace projectname , image name and token to yours

2
Jon Shier On

First, your request headers have nothing do with whether the response is valid, so adding image/jpeg there won't help. Second, your response is return an application/json content type, so adding image/jpeg to the acceptable content types won't help (and image/jpeg is already acceptable). (As an aside, you need to import AlamofireImage to get addAcceptableImageContentTypes to work.)

In the end, you should ensure you're getting the response you expect, as an application/json content type implies you're getting a JSON body, not an image fro your request. When I request the URL you posted, I get this JSON response:

{
  "error": {
    "code": 400,
    "message": "Invalid HTTP method/URL pair."
  }
}

So most likely you need to double check how you're supposed to be making the request.