Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 638
M to the K Avatar asked Dec 06 '13 10:12

M to the K


People also ask

How do I manage HTTP response headers?

Use the HTTP Response Headers feature page to manage a list of name and value pairs that contain information about a requested page, and to configure common HTTP headers. Sort the list by clicking one of the feature page column headings or select a value from the Group by drop-down list to group similar items.

What is afnetworking/afnetworking?

GitHub - AFNetworking/AFNetworking: A delightful networking framework for iOS, macOS, watchOS, and tvOS. Loading status checks… Failed to load latest commit information. Exempt "needs investigation" from stalebot. Fix redirect test: use httpbingo service instead due to postmanlabs/h…

How do I add afnetworking to my Xcode project?

AFNetworking supports multiple methods for installing the library in a project. To integrate AFNetworking into your Xcode project using CocoaPods, specify it in your Podfile: Once you have your Swift package set up, adding AFNetworking as a dependency is as easy as adding it to the dependencies value of your Package.swift.

How do I report a security vulnerability with afnetworking?

And most of all, thanks to AFNetworking's growing list of contributors. If you believe you have identified a security vulnerability with AFNetworking, you should report it as soon as possible via email to [email protected]. Please do not post it to a public issue tracker.


1 Answers

a slightly more robust code than Viruss mcs's:

if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
    NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
    NSLog(@"%@" ,[r allHeaderFields]);
}

returns

{
    Connection = "Keep-Alive";
    "Content-Length" = 12771;
    "Content-Type" = "application/json";
    Date = "Fri, 06 Dec 2013 10:40:48 GMT";
    "Keep-Alive" = "timeout=5";
    "Proxy-Connection" = "Keep-Alive";
    Server = "gunicorn/18.0";
}

similarly you can assure the casting is done right with [response respondsToSelector:@selector(allHeaderFields)], but you should also call that before you do the cast

if ([task.response respondsToSelector:@selector(allHeaderFields)]) {
    NSHTTPURLResponse *r = (NSHTTPURLResponse *)task.response;
    NSLog(@"%@" ,[r allHeaderFields]);
}

or no cast at all:

if ([task.response respondsToSelector:@selector(allHeaderFields)]) {
    NSLog(@"%@" ,[task.response performSelector:@selector(allHeaderFields)]);
}
like image 92
vikingosegundo Avatar answered Sep 30 '22 01:09

vikingosegundo