iosobjective-ccocoa-touchios7afnetworking-2

AFNetworking 2.0 // How to read response header


I'm using the new version of AFNetworking and I can't figure out how to read the headers of the response. I'm using the AFHTTPSessionManager to perform my query, everything works well but I'm unable to find the header response field.

Here is how I proceed

self.sessionManager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
[self.sessionManager GET:urlId parameters:nil
    success:^(NSURLSessionDataTask *task, id responseObject) {
        if ([self.delegate respondsToSelector:@selector(userIsLoadedWithInfos:)]) {
            [self.delegate userIsLoadedWithInfos: responseObject];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if ([self.delegate respondsToSelector:@selector(userLoadingFailed)]) {
            [self.delegate userLoadingFailed];
        }
    }
];

I tried to read the response attribute of task but it return an NSURLResponse which doesn't include the headers.

Does anyone now how to read the response headers with the 2.0 version?


Solution

  • Have you tried to get headers from NSURLResponse which is return,

    You can try something like with NSURLResponse object,

    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    if ([httpResponse respondsToSelector:@selector(allHeaderFields)]) {
        NSDictionary *dictionary = [httpResponse allHeaderFields];
        NSLog([dictionary description]);
    }
    

    Hope This will Help You.!