Search code examples
iosobjective-cafnetworkingafnetworking-2

AFNetwork 401 error code in ios


I'm having problem with an AFNetwork2. I get the error code 401.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.securityPolicy.allowInvalidCertificates = NO;
manager.requestSerializer = [AFJSONRequestSerializer serializer];

[manager.requestSerializer setValue:authorization forHTTPHeaderField:@"Authorization"];
[manager.requestSerializer setValue:Accept forHTTPHeaderField:@"Accept"];


[manager POST:apiUrl parameters:nil
      success:^(AFHTTPRequestOperation *operation, id responseObject) {
          NSLog(@"POST data JSON returned: %@", responseObject);
      }
      failure:^(AFHTTPRequestOperation *operation, NSError *error) {
          NSLog(@"Error: %@", error);
      }
 ];

I added the networktransport to the plist as well.

I checked the API with postman. I did all I had to do. So I hope you will be able to help me.


Solution

  • Http status code 401 means unauthorised. Are you sure you are sending correct authorization value while calling api? Their is a possibility that your authorization code might have expired, trying regenerating a new authorization code, also check you are setting correct key for header fields. Code which you have written is absolutely fine. 401 just means you are not authorized to call that api.