0

I have to pass Authorization token in NSMutableURLRequest , My query is Whenever I have to pass Correct token than I am getting good response with https Status (200.. etc). but whenever I have to pass wrong Authorization token Than I am getting following response insted of http status code 401

* with wrong token response * (I had hide my query htpp:....)

Error Domain=NSURLErrorDomain Code=-1012 "(null)" UserInfo={NSErrorFailingURLStringKey=http......, NSUnderlyingError=0x60800024c420 {Error Domain=kCFErrorDomainCFNetwork Code=-1012 "(null)" UserInfo={_kCFURLErrorAuthFailedResponseKey={url = http.....}}}, NSErrorFailingURLKey=http....}

** here is my code ** // parameter in dictionary

   NSDictionary *parameters = @{ @"user_id": [NSNumber numberWithInt:1]};

   NSError *error;
   NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                                                   options:NSJSONWritingPrettyPrinted
                                                     error:&error];

   // Convert dictionary to Json String

   if (! jsonData) {
    NSLog(@"Got an error: %@", error);
    } else {
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

    jsonparameter = jsonString;

}

NSData *postData = [jsonparameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

 NSMutableURLRequest *request = [NSMutableURLRequest
                                requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:SiteAPIURL,wsname]]];

    NSString *Accesstoken = [NSString stringWithFormat:@"Bearer %@",tokenInfo.access_token];

    // Set Access token 

    [request setValue:Accesstoken forHTTPHeaderField:@"Authorization"];

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSHTTPURLResponse __autoreleasing *response;
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"%@",error.localizedDescription);
NSLog(@"Status code =%ld",(long)response.statusCode);
Arjun Patel
  • 1,394
  • 14
  • 23
  • Completely unrelated, but (a) you don't have to set `Content-Length` header as that's done for you; (b) it's unnecessary to take `jsonData`, convert it to a string and then convert it back to a `NSData`. Just set `HTTPBody` to be `jsonData`; (c) unless you need to support OS versions predating `NSURLSession`, you should really retire `NSURLConnection` and not do synchronous requests. – Rob Sep 05 '17 at 20:04
  • You did not receive a -1012 status code. That is the error code, which is `NSURLErrorUserCancelledAuthentication`. You’re confusing the error with the status code that you print on the next line. What was a status code printed after the error? – Rob Sep 05 '17 at 20:10
  • after error I am getting Status code = null – Arjun Patel Sep 06 '17 at 04:29
  • OK, then clearly the authorization error is preventing the `NSHTTPURLResponse` from capturing any status code. – Rob Sep 06 '17 at 05:08
  • Can you please explain briefly , What I do to get http status code 401 ?, Thanks – Arjun Patel Sep 06 '17 at 06:00
  • I get a 401 when I try to login to a web server with BASIC authentication using `URLSession` but supply incorrect (or no) credentials. I don't know if you're not seeing 401 because you're using `URLConnection` or because your server is using BEARER authentication. (I'd guess the latter.) I guess if you know how to detect this (error code `NSURLErrorUserCancelledAuthentication`), isn't that sufficient? – Rob Sep 06 '17 at 06:43

1 Answers1

0

401 is server generated error. -1012 is system generated error. This sometimes may be because of app is keeping cached certificates and certificate on the server has been changed so they don't match. Clearing Caches and Preferences folders from app's home directory will delete the old certificate data and app will have to download the new one from the server and you problem will be solved.

Garnik
  • 423
  • 1
  • 6
  • 20