Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request failed: unauthorized (401) - AFNetworking iOS

I am trying to make a webservice call in which i am sending some input data and getting cookies. for the subsequent calls, i am using cookies which i got from the last call and make a new call. But unfortunately i got 401 error. But when i try this in another system i got a response.

Error Domain=AFNetworkingErrorDomain Code=-1011 "Request failed: unauthorized (401)" UserInfo=0xb469a60 {NSErrorFailingURLKey=https://<base url>/<tenant>/<resource>, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb550b60> { URL: https://<base url>/<tenant>/<resource> } { status code: 401, headers {
    "Cache-Control" = private;
    "Content-Length" = 0;
    Date = "Wed, 04 Dec 2013 08:55:47 GMT";
    Server = "Microsoft-HTTPAPI/2.0";
    "X-AspNet-Version" = "4.0.30319";
    "X-Powered-By" = "ASP.NET";
} }, NSLocalizedDescription=Request failed: unauthorized (401)}

This is my post request

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    AFHTTPResponseSerializer *serializer = [AFHTTPResponseSerializer serializer];
    serializer.acceptableContentTypes = @"application/json";

    manager.responseSerializer = serializer;
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
  [manager POST:path parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSURL* networkAddress = [NSURL URLWithString:baseServerURL];
                NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:networkAddress];
                if (cookies.count > 0)
                {
                    [self saveCookies];
                    success(responseObject);
                }
                else
                {
                    failure([NSError errorWithDomain:@"App" code:1 userInfo:@{@"error": @"An Error Occured"}]);

                }

            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [self clearCookies];
                failure (error);
            }];

I tried resetting my simulator and repened my xcode. but nothing seems to be help. But in another system i am able to get all data.

PS: i am making this call after login. so unauthorized access seems to be invalid. i think it is a problem from client side.

Any help is appreciated,

like image 346
iPhone Guy Avatar asked Jun 07 '26 04:06

iPhone Guy


1 Answers

I'm having the exact same issue, the only way I've found that works is calling your request inside the success block of your request to login. It sucks but I've spent hours trying everything I can to get it to work. Will update if I find out how.

[[AFAppDotNetAPIClient sharedClient] POST:@"users/login" parameters:loginDetails success:^(NSURLSessionDataTask * __unused task, id JSON) {

        [[AFAppDotNetAPIClient sharedClient] POST:@"posts" parameters:nil success:^(NSURLSessionDataTask * __unused task, id JSON) {


        } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {


        }];

    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {


    }];

UPDATE: Yeah the emulator kind of doesn't work.... tested on a device and was fine.

like image 98
michael.boost Avatar answered Jun 10 '26 02:06

michael.boost