AFTNetworking with Token to download image

0

I'm testing AFTNetworking to download images from a backend, however I'm having errors downloading images.

I have tried several implementations, with this implementation I have not accepted content error.

Below is the last implementation I tested:

NSString* urlImagem = @"http://lojacliente.goldarkapi.com/pizzas/54bf3e3a657f6f2c04a81afd/imagem";

AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];

operationManager.responseSerializer = [AFImageResponseSerializer serializer];

operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];


[operationManager.requestSerializer setValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];
[operationManager.requestSerializer setValue:@"zRWGVZ0HEpSzsb1ZOYMYHCwL6q54qa2x3y3JlNmCN61d1SCmfRDfnbFHteu/R50N29bOQJv8IgG6aLD4y8dNWQ==" forHTTPHeaderField:@"X-Api-Token"];

[operationManager GET:urlImagem parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"success");

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    NSLog(@"failure. Error:%@",error);

}];

ERROR:

  

GDRequestImageAFTNetworking [1647: 65915] failure. Error: Error   Domain = com.alamofire.error.serialization.response Code = -1016 "Request   failed: unacceptable content-type: image / jpeg "UserInfo = 0x78604030   {com.alamofire.serialization.response.error.response = {URL:    link   } {status code: 200, headers {       "Access-Control-Allow-Credentials"="true, true";       "Access-Control-Allow-Headers"="Content-Type, Content-Length, Depth, User-Agent, X-Requested-With, X-   If-Modified-Since, X-Access-Token, X-FileEncoding, Cache-Control ";       "Access-Control-Allow-Methods"="GET, PUT, POST, DELETE, OPTIONS, HEAD";       "Access-Control-Allow-Origin"="*";       Connection="keep-alive";       "Content-Length" = 25553;       "Content-Type"="image / jpeg";       Date="Fri, 30 Jan 2015 16:49:36 GMT";       Etag="\" 5c08b1f8fc6e9943027d4062badbd5d60aca31f0 \ "";       Server="nginx / 1.4.1 (Ubuntu)"; }}, NSErrorFailingURLKey = link ,   NSLocalizedDescription = Request failed: unacceptable content-type:   image / jpeg, com.alamofire.serialization.response.error.data =

How could I correctly set up the request header with Token?

    
asked by anonymous 30.01.2015 / 17:34

2 answers

0

I was able to download using the following code:

     AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];

        operationManager.responseSerializer = [AFImageResponseSerializer serializer];

        [operationManager.requestSerializer setValue:@"TOKEN" forHTTPHeaderField:@"X-Api-Token"];

        [operationManager GET:urlImagem parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

// responseObject será um valor do tipo UIImage, dispensando qualquer conversão de data 
// para image. Podendo ser usado diretamente ou inserido em um UIImage.

            NSLog(@"success");


        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

            NSLog(@"failure. Error:%@",error);

        }];
    
30.01.2015 / 18:04
2

The problem is that you are sending the headers HTTP through the GET parameters.

To set these headers you should use setValue:forHTTPHeaderField: of AFHTTPRequestSerializer

You can do this:

[operationManager.requestSerializer setValue:@"image/jpg" forHTTPHeaderField:@"Content-Type"];
[operationManager.requestSerializer setValue:@"_TOKEN_" forHTTPHeaderField:@"X-Api-Token"];
    
30.01.2015 / 17:46