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?
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.
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…
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.
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.
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)]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With