Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading AFNetworking response headers

I'm trying to figure out how to read the response headers from a AFNetworking request?

Is it possible in the following snippet, or do I need to take another approach?

// Create client
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://example.com/"]];

// Send request
[client getPath:@"/test" parameters:nil success:^(AFHTTPRequestOperation *operation, id response) {

} failure:^(AFHTTPRequestOperation *operation, NSError *error){

}];
like image 640
lix Avatar asked Sep 10 '12 12:09

lix


2 Answers

The easiest way to accomplish this is to use the response property (not the response object of the block) of the AFHTTPRequestOperation instance that is available in both the success and failure blocks.

This response object is an instance of NSHTTPURLResponse and you can send it a message of allHeaderFields to fetch all the headers of your request.

like image 58
Bart Jacobs Avatar answered Oct 03 '22 22:10

Bart Jacobs


Quite simply, since the accepted answer doesn't actually have an example:

[operationInstance setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
       NSLog(@"%@", operation.response.allHeaderFields);
}];
like image 41
brandonscript Avatar answered Oct 04 '22 00:10

brandonscript