0

I use the following code to make a GET request including a header Authorization but does not seem to work...the get request does not include the authorization token. Any ideas?

   // 1 - define resource URL
    
    NSURL *URL = [NSURL URLWithString:@"https://myurl"];
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    AFHTTPRequestSerializer *requestSerializer = [AFHTTPRequestSerializer serializer];
    [requestSerializer setValue:[NSString stringWithFormat:@"Bearer %@",Token] forHTTPHeaderField:@"Authorization"];
    
   manager.requestSerializer = requestSerializer;

    //3 - set a body
    NSDictionary *body =@{@"email":@"a@gmail.com"};
    //4 - create request
    [manager GET:URL.absoluteString
       parameters:body
       
         progress:nil
     //5 - response handling
          success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        
            NSLog(@"Reply POST JSON: %@", responseObject);

    }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error on machine token: %@", error);
    }
    ];
stefanosn
  • 3,264
  • 10
  • 53
  • 79
  • Is it really a "Bearer" Authorization token? See https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml for possible ones. Is that what says your documentation? – Larme Nov 05 '21 at 08:09
  • @Larme I get this error back all the time Status Code: 400, Headers { "Content-Length" = ( 915 ); "Content-Type" = ( "text/html" ); Date = ( "Fri, 05 Nov 2021 17:25:44 GMT" ); Server = ( CloudFront ); Via = ( "1.1 323.cloudfront.net (CloudFront)" ); "x-amz-cf-id" = ( "wRw==" ); "x-amz-cf-pop" = ( "ATH" ); "x-cache" = ( "Error from cloudfront" ); } }} – stefanosn Nov 05 '21 at 17:32
  • @Larme Yes it is a Bearer checked it using NSMutableURLRequest and it works. For some reason does not work with the implementation i posted! – stefanosn Nov 07 '21 at 15:31
  • Maybe giving the "altenative `NSMutableURLRequest` working code" might help find out the issue... And what about using this method https://github.com/AFNetworking/AFNetworking/blob/77ef5fed64d98107acd177a90182163a20ba4567/AFNetworking/AFHTTPSessionManager.h#L145 which has a `header` parameter? – Larme Nov 07 '21 at 16:10
  • @Larme Yeah this is what i have used by AFNetworking NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request]; I just pass the request which is NSMutableRequest but i am just curious why the above implementation does not work. – stefanosn Nov 07 '21 at 16:19

0 Answers0