Search code examples
iphoneobjective-chttp-redirectrestkit

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]);
}

Solution

  • 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.