SpaceDust__
SpaceDust__

Reputation: 4914

AFNetworking Expected status code in (200-299), got 403

Trying to migrate my code from ASIHttpRequest to AFNetworking. It seems similar questions has been asked but couldnt find solution to my problem.

My code was working fine with ASIHttpRquest.

I send a simple post request to my server and listen http responses. If http response is 200 everything works fine but if I send another status code >400 AFNetworking block fails.

Server side response:

$rc = $stmt->fetch();
    if ( !$rc ) {
    //  echo "no such record\n";
      $isrecordExist=0; //false does not exists
      sendResponse(403, 'Login Failed');
      return false;
    }
    else {
     // echo 'result: ', $result, "\n";
       $sendarray = array(
            "user_id" => $result,
        );
        sendResponse(200, json_encode($sendarray));
    }

IOS Part:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:
                            [NSURL URLWithString:server]];
    client.allowsInvalidSSLCertificate=YES;

    [client postPath:loginForSavingCredientials parameters:params success:^(AFHTTPRequestOperation *operation, id response) {
    if (operation.response.statusCode == 500) {}
     else if (operation.response.statusCode == 403) {}
     else if (operation.response.statusCode == 200) {//able to get results here            NSError* error;
        NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSDictionary* json =     [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                                                 options: NSJSONReadingMutableContainers
                                                                   error: &error];}
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failure %@", [error localizedDescription]);
    }];

NSLOG:

 failure Expected status code in (200-299), got 403

How can I fix this?

Upvotes: 2

Views: 14937

Answers (2)

Craig Siemens
Craig Siemens

Reputation: 13296

When AFNetworking gets a 2xx (success) status code, it calls the success block.

When it gets a 4xx (client error) or 5xx (server error) status code, it calls the failure block because something went wrong.

So all you should need to do is move your check for a 500 or 403 status code to the failure block.

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:server]];
client.allowsInvalidSSLCertificate=YES;

[client postPath:loginForSavingCredientials parameters:params success:^(AFHTTPRequestOperation *operation, id response) {
    if (operation.response.statusCode == 200) {//able to get results here            NSError* error;
        NSString *responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
        NSDictionary* json = [NSJSONSerialization JSONObjectWithData: [responseString dataUsingEncoding:NSUTF8StringEncoding]
                                                             options: NSJSONReadingMutableContainers
                                                               error: &error];
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"failure %@", [error localizedDescription]);
    if (operation.response.statusCode == 500) {}
    else if (operation.response.statusCode == 403) {}
}];

Upvotes: 12

Wain
Wain

Reputation: 119041

When you create the request operation you need to tell it which response status codes are acceptable (mean success). By default this is codes in the range 200 -> 299.

Setup before you start using the client:

AFHTTPRequestOperation.acceptableStatusCodes = ...;

[client postPath:

Docs are here.

Upvotes: 1

Related Questions