0

I want to get data from my backend. I should set a request header with Authorization and use the generate auth_token from my previous interaction to the backend. here it is my curl

curl -H 'Authorization: Token token="replace-with-token"' http://domain.com/books

here it is my code

  NSURL *url = [NSURL URLWithString:@"http://domain.com/books"];
    config = [NSURLSessionConfiguration defaultSessionConfiguration];
    [config setHTTPAdditionalHeaders:@{@"token":@"4959a0bc00a15e335fb6"}];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    [[session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@", [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]);
    }] resume];

I am not sure if I implemented the curl correctly. The data,which I received from backend, is null . Does any one know the reason?

MKH
  • 153
  • 1
  • 3
  • 15
  • here it is the right answer: *http://stackoverflow.com/questions/31344118/how-to-set-the-authorization-token-based-on-this-curl* – MKH Jul 10 '15 at 16:24

1 Answers1

1

A curl header of 'Authorization: Token token="replace-with-token"' would translate into:

[config setHTTPAdditionalHeaders:@{@"Authorization":@"token=\"4959a0bc00a15e335fb6\""}];
Rudi Angela
  • 1,463
  • 1
  • 12
  • 20