-1

I need to call the service with pass the access token in the header.

But i thing the token is not going on the header. I am not getting the responce. what am i doing wrong?

Code

 +(void)getUserwithToken:(NSString *)token{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://exapmle.com/api/users"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:[NSString stringWithFormat:@"Bearer %@", token] forHTTPHeaderField:@"Authorization"];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSString *str1 = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str1);
    }];
}

OutPut :connectionError : (null) , str1 : {}

Raj Sharma
  • 11
  • 4
  • You need to do some debugging: If you are getting an error `NSLog(@"connectionError: %@", connectionError)` and add the result to the question. Also take a look at the network request with Charles Proxy. – zaph Jan 26 '15 at 18:17
  • I have updated the question. i just want to know if i am doing wrong any. because if i pass header or not responce is same. So may be service is not getting the header – Raj Sharma Jan 26 '15 at 18:25
  • Is the value in the correct format? – zaph Jan 26 '15 at 19:01

1 Answers1

0

There are a couple things wrong with the request:

  1. You are not checking for and handling error conditions.
  2. You are issuing a POST request but there is not body (data), generally such requests are issued as GET requests.
zaph
  • 111,848
  • 21
  • 189
  • 228