kailoon
kailoon

Reputation: 2069

Detect a redirect in RestKit not working?

I have a web service returning a 302 and the automatic redirect is done by RestKit. I'm trying to do extra logic before the redirect. I know RestKit has isRedirect and isRedirection. So in the request:didLoadResponse delegate I tried to check for it, but it doesn't seem to be hitting any of those. Is there something I'm missing?

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response 
{
    if ([response isRedirection]) 
    {
        NSLog(@"redirection");
    } 
    else if ([response isRedirect])
    {
        NSLog(@"redirect");
    }
    NSLog(@"response %@", [response bodyAsString]);
}

Upvotes: 1

Views: 1009

Answers (3)

biddster
biddster

Reputation: 678

An old thread, I know, but just in case anyone comes looking. If you're using RKObjectManager, you can implement setRedirectResponseBlock in order to ignore/modify redirects:

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:url];
RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:request ...etc...];
[operation.HTTPRequestOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
        return redirectResponse ? nil : request;
    }];
[objectManager enqueueObjectRequestOperation:operation];

Seems to do the trick.

Upvotes: 0

Stefano Mondino
Stefano Mondino

Reputation: 1219

I've found an alternative to kailoon way that shoud be a little less "code invasive" for everyone having problems with that solution. use

[request setFollowRedirect:NO]

and you should be able to trap the 302 in the requestDidReceiveResponse callback. It worked for me.

Upvotes: 2

kailoon
kailoon

Reputation: 2069

Solved this by implementing this:

@implementation RKResponse (CustomRedirect)

- (NSURLRequest *)connection:(NSURLConnection *)inConnection willSendRequest:(NSURLRequest *)inRequest redirectResponse:(NSURLResponse *)inRedirectResponse 
{
    NSLog( @"redirectResponse ***********************************************");

    NSURLRequest *newRequest = inRequest;
    if (inRedirectResponse) {
        newRequest = nil;
    }
    return newRequest;
}

@end

This basically says you want to handle the redirect yourself since you are returning nil. Now the checks for isRedirect and isRedirection will be hit.

Upvotes: 2

Related Questions