Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking 2.2.1 loading an image from Amazon S3 server

I'm running into a problem trying to download an image on an Amazon S3 server.

I get the following error:

Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: binary/octet-stream" 

Anyone has an idea?

like image 657
James Laurenstin Avatar asked Mar 23 '14 00:03

James Laurenstin


1 Answers

This error is generated by

- (BOOL)validateResponse:(NSHTTPURLResponse *)response
                    data:(NSData *)data
                   error:(NSError * __autoreleasing *)error

method of AFHTTPResponseSerializer in case of unexpectable MIME type of response.

You can fix it by adding required MIME type to response serializer

// In this sample self is inherited from AFHTTPSessionManager
self.responseSerializer = [AFImageResponseSerializer serializer];
NSSet *set = self.responseSerializer.acceptableContentTypes;
self.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"binary/octet-stream"];

Or you can modify AFImageResponseSerializer :

- (instancetype)init {
    self = [super init];
    if (!self) {
        return nil;
    }

    self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"image/tiff", @"image/jpeg", @"image/gif", @"image/png", @"image/ico", @"image/x-icon", @"image/bmp", @"image/x-bmp", @"image/x-xbitmap", @"image/x-win-bitmap", @"binary/octet-stream", nil];

#if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
    self.imageScale = [[UIScreen mainScreen] scale];
    self.automaticallyInflatesResponseImage = YES;
#endif

    return self;
}

But root of the problem is probably that you save your images to Amazon with wrong MIME type or without type at all. In my code I save images to Amazon with following code

S3PutObjectRequest *putObjectRequest = [ [ S3PutObjectRequest alloc ] initWithKey:keyImage    inBucket:self.s3BucketName ];
putObjectRequest.contentType = @"image/jpeg";
putObjectRequest.data = UIImageJPEGRepresentation( [ image fixOrientation ], 0.5f );
putObjectRequest.cannedACL = [ S3CannedACL publicRead ];
like image 199
Avt Avatar answered Oct 18 '22 20:10

Avt