paulmiard
2/12/2014 - 8:42 PM

Example for getting "Link" HTTP header using AFNetworking

Example for getting "Link" HTTP header using AFNetworking

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:@"https://api.github.com/search/code?q=addClass+user:mozilla&page=4&per_page=42" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //NSLog(@"JSON: %@", responseObject);
    
    NSDictionary *headers = operation.response.allHeaderFields;
    //NSLog(@"Headers: %@", [headers description]);
    NSLog(@"Link: %@", [headers objectForKey:@"Link"]);
    
    //Will look like Link: <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=5&per_page=42>; rel="next", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=24&per_page=42>; rel="last", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=1&per_page=42>; rel="first", <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=3&per_page=42>; rel="prev"
        
    //Below parser examples in other languages. Didn't find an Objective-C version but easy enough to port
    // Java: https://github.com/eclipse/egit-github/blob/master/org.eclipse.egit.github.core/src/org/eclipse/egit/github/core/client/PageLinks.java#L43-75
    // JS: https://gist.github.com/niallo/3109252
    // Ruby (last section): http://developer.github.com/guides/traversing-with-pagination/
        
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];