0

I'm requesting an API with AFNetworking 4.3.1 and Http method GET and HEAD. It could return different http status code (100, 200, 402, ..)

My get function request working fine.

-(void) getApiV2:(NSString *)url
      parameters:(NSDictionary *)param
         success:(successBlock)mSuccessBlock
         failure:(failureBlock)mfailureBlock{

    NSMutableDictionary *httpHeader =[NSMutableDictionary dictionary];
    if(TOKEN !=nil){
        [httpHeader setObject:TOKEN forKey:@"Token"];
    }

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager
        GET:url
        parameters:param
        headers:httpHeader
        progress:nil
        success: ^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject){
            int statusCode = [[responseObject objectForKey:@"code"] intValue];
            NSArray *data = [responseObject objectForKey:@"data"];
            BLOCK_SAFE_RUN(mSuccessBlock,data,statusCode);
        }
        failure: ^(NSURLSessionDataTask * dataTask, NSError * error){
            BLOCK_SAFE_RUN(mfailureBlock,error);
        }
    ];
};

But my head function throw error.

-(void) head:(NSString *)url success:(successBlock)mSuccessBlock failure:(failureBlock)mfailureBlock{

    NSMutableDictionary *httpHeader =[NSMutableDictionary dictionary];
    if(TOKEN !=nil){
        [httpHeader setObject:TOKEN forKey:@"Token"];
    }

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager
        HEAD:url
        parameters:nil
        headers:httpHeader
        success: ^(id _Nullable responseObject){
            int statusCode = [[responseObject objectForKey:@"code"] intValue];
            BLOCK_SAFE_RUN(mSuccessBlock,responseObject,statusCode);
        }
        failure: ^(NSURLSessionDataTask * dataTask, NSError * error){
            BLOCK_SAFE_RUN(mfailureBlock,error);
        }
    ];
};

At run time, I have this error on the line int statusCode = [[responseObject objectForKey:@"code"] intValue];

[__NSCFLocalDataTask objectForKey:]: unrecognized selector sent to instance 0x110f88860

The server properly handle the request and do it job.


I try other syntax:

int statusCode = (int) [responseObject statusCode];

'NSInvalidArgumentException', reason: '-[__NSCFLocalDataTask statusCode]: unrecognized selector sent to instance 0x145731af0'

from SO

            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)responseObject;
            int statusCode = (int) httpResponse.statusCode;

[__NSCFLocalDataTask statusCode]: unrecognized selector sent to instance 0x129665ed0


What is the good way to get the http status code for a HEAD request with AFNetworking?

europrimus
  • 53
  • 5
  • `responseObject` is a `NSCFLocalDataTask`, which should be, I guess a `URLSessionTask`. Try to print it first. `NSLog(@"Response tasks: %@", responseObject);` But then, it's that's one: `URLSessionTask *task = (URLSessionTask *)responseObject; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)[task response]; int statusCode = (int) httpResponse.statusCode;` – Larme Aug 20 '20 at 11:05
  • @Larme, Thanks for your help. now its work with ` NSURLSessionTask *task = (NSURLSessionTask *)responseObject; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)[task response]; int statusCode = (int) httpResponse.statusCode; NSLog(@"Status code: %d", statusCode);` You could write an answer so I approve it. – europrimus Aug 20 '20 at 12:18

1 Answers1

0

As suggested by @Larme and confirmed by the source code of AFNetworking, HEAD:parameters:headers:success:failure: return a NSURLSessionDataTask

So I need to retrieve the NSHTTPURLResponse before I can get the statusCode.

NSURLSessionTask *task = (NSURLSessionTask *)responseObject;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)[task response];
int statusCode = (int) httpResponse.statusCode;
NSLog(@"Status code: %d", statusCode);
europrimus
  • 53
  • 5