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?