e7mac
e7mac

Reputation: 553

Using AFNetworking on iOS, not getting a 302 status code in redirect

I am using AFNetworking on iOS and trying to access webpage that gives me a 302 status code, redirects me and then a 200 when the redirected page loads.

However, I am unable to capture this in my iOS app. I tried adding the authenticationChallenge block but it doesn't get called. The completionBlock is getting the redirected view but a status code of 200 only. What am I missing here? I need to send login details when I get a 302 code. My code is pasted below:

  AFHTTPRequestOperation *uploadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:myRequest];
[uploadOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id response) {
    NSLog(@"Request: %@", [operation.request description]);
    NSLog(@"CODE: %i",operation.response.statusCode);
    NSLog(@"Response: %@", [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    self.statusLabel.text = @"upload failed";
}];    

[uploadOperation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
    NSLog(@"pls authenticate");
}];

Upvotes: 2

Views: 3059

Answers (1)

e7mac
e7mac

Reputation: 553

Got it. I was setting the wrong block. Authentication challenge isn't what the Django app sends in case of the @login_required decorator. It's the redirectResponse that needs to be set!

[uploadOperation setRedirectResponseBlock:^NSURLRequest *(NSURLConnection *connection, NSURLRequest *request, NSURLResponse *redirectResponse) {
    NSLog(@"pls authenticate");
    return request;
}];

Upvotes: 3

Related Questions