Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking: set GET params *and* intercept redirects

I'm using AFNetworking 2.0 in an iOS project, and I'm trying to build a GET request with some parameters, and intercept redirects.

I see the method -[AFHTTPRequestOperation setRedirectResponseBlock], which I'm using to grab the redirects and do something with them. But I don't see how to set the request parameters on that operation. Here's what that looks like:

    AFHTTPRequestOperation *ballotOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest];

    [ballotOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"in completion");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"in error");
    }];

    [ballotOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
        if (redirectResponse == nil) {
            return request;
        } else {
            NSLog(@"in redirect, blocking");
            [ballotOperation cancel];
            return nil;
        }
    }];

    [[AFHTTPRequestOperationManager manager].operationQueue addOperation:ballotOperation];

I see AFHTTRequestOperationManager has the method GET:parameters:success:failure: in which you can set the parameters. But that starts the request immediately, not giving me a chance to set the redirect block on it.

I see some sample code out there from AFNetworking 1.x using AFHTTPClient, but I don't want to go back!

How can I do what I'm trying to do?

like image 617
dpassage Avatar asked Jan 15 '14 07:01

dpassage


1 Answers

The [AFHTTPRequestOperationManager GET...] method in AFHTTPRequestOperationManager.m is just a wrapper around creating a AFHTTPRequestOperation object and adding it to the operationQueue. Using this as an example, you can accomplish what you are trying to do.

This is how the request is created in the GET method of AFHTTPRequestOperationManager:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"GET" URLString:[[NSURL URLWithString:URLString relativeToURL:manager.baseURL] absoluteString] parameters:parameters error:nil];

where urlString is a NSString representing the url, and parameters are an NSDictionary.

I believe the rest of your code should work, but just in case, here is how it is done in the GET method (with your redirect block added as well):

AFHTTPRequestOperation *ballotOperation = [self HTTPRequestOperationWithRequest:request success:success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"in completion");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"in failure");
}];

[ballotOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
    if (redirectResponse == nil) {
        return request;
    } else {
        NSLog(@"in redirect, blocking");
        [ballotOperation cancel];
        return nil;
    }
}];

[manager.operationQueue addOperation:ballotOperation];
like image 76
roob Avatar answered Nov 13 '22 08:11

roob