2

I am getting "401 Unauthorized access to the endpoint". When I run the same call on POSTMAN (Chrome Extension), it works fine. Even it works fine in my Android app.

But here, I am getting the unauthorized access even if I am sending token in headers. I have no idea what is going wrong here. The response I am getting is:

{"detail":"Authentication credentials were not provided."}

Here is my code:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *url=[NSString stringWithFormat:@"%@",endpoint];
NSLog(@"url is %@",url);
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *token=[NSString stringWithFormat:@"token %@",TOKEN];
NSLog(@"token is %@",token);
[request addValue:token forHTTPHeaderField:@"Authorization"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"requestReply: %@", requestReply);
}] resume];

What and where am I doing something wrong?

Edit: If I tried this way by using different class, the response is (null).

NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL
                                     URLWithString:url]
                        cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                    timeoutInterval:10
 ];
[request setHTTPMethod: @"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSString *token=[NSString stringWithFormat:@"token %@",[[NSUserDefaults standardUserDefaults]valueForKey:D_KEY_TOKEN]];
[request setValue:token forHTTPHeaderField:@"Authorization"];
NSError *requestError = nil;
NSURLResponse *urlResponse = nil;
[NSURLConnection sendSynchronousRequest:request
                      returningResponse:&urlResponse error:&requestError];
NSLog(@"url response is %@",urlResponse);

Edit2:

NSString *url=[NSString stringWithFormat:@"%@/",endpoint];

This is what I have done just append the slash(/)at the end of endpoint at it starts working. I want to know why there is a need to append slash at the end? All my other calls are working without slash.

Nav
  • 435
  • 1
  • 4
  • 15
  • 1
    Should the value for the "AUTHORIZATION" header have the text `token ` prepended to the actual token value? – rmaddy May 15 '16 at 21:02
  • yes it should. That is how we are sending the authorization to the server. – Nav May 15 '16 at 22:26
  • @fragilecat No. We are just sending Authorization in Header. We are not using Auth2. Moreover, I don't know about the different types of authorization. I can see them as options in postman but we are not using any of those. – Nav May 16 '16 at 02:47
  • Have you compared the header you send in postman/Android with what the iOS app is sending? – Peter Hornsby May 16 '16 at 11:09
  • Try switching NSString *token=[NSString stringWithFormat:@"token %@",TOKEN]; to NSString *token=[NSString stringWithFormat:@"Bearer %@",TOKEN]; If you can authenticate via a http header then you are using OAuth2. – Peter Hornsby May 16 '16 at 11:40
  • I did compare the header in Android/postman. In fact, I copied token from here and paste it in postman. Worked fine. I tried the one with bearer, did not make any difference. – Nav May 16 '16 at 15:33
  • I got this working by adding / at the end of the endpoint. My url looks like url/id/ now. Can anybody explain why it starts working after appending slash. – Nav May 16 '16 at 19:46

1 Answers1

5

Try to put a slash "/" at the end of the url.

Caio Dias
  • 66
  • 1