Aftnetworking with Token to download image

Asked

Viewed 237 times

0

I’m testing Aftnetworking to download images from a backend, but I’m having errors downloading images.

I tested several implementations, with this implementation presents error of content not accepted.

Below is the last implementation I did of testing:

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] Ailure. 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: http://lojacliente.goldarkapi.com/pizzas/54bf3e3a657f6f2c04a81afd/imagem } { 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-Requested-By, 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=http://lojacliente.goldarkapi.com/pizzas/54bf3e3a657f6f2c04a81afd/imagem, Nslocalizeddescription=Request failed: Unacceptable content-type: image/jpeg, com.alamofire.serialization.response.error.data=

How could you correctly set the request header setting with Token?

  • What happens when you remove the content-type of the requisition?

  • James, if after applying the suggestions to correct your question there was another error, there was no need to change your whole question, causing the André was out of coherence. You could have just added the information of the new error in the question.

  • Yes sorry. It wasn’t intensional. @Andréribeiro, thank you, your reply helped me refine the result.

2 answers

2

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

To set these headers you must use the method setValue:forHTTPHeaderField: of AFHTTPRequestSerializer

You can do it like this:

[operationManager.requestSerializer setValue:@"image/jpg" forHTTPHeaderField:@"Content-Type"];
[operationManager.requestSerializer setValue:@"_TOKEN_" forHTTPHeaderField:@"X-Api-Token"];
  • Updated issue.

  • I already tried to use setValue:forHTTPHeaderField, but it also didn’t work... I made some changes and got it. It was some parameters I put in that were unnecessary.

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);

        }];

Browser other questions tagged

You are not signed in. Login or sign up in order to post.