MKH
MKH

Reputation: 153

How do I convert this curl command to Objective-C?

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?

Upvotes: 0

Views: 245

Answers (1)

Rudi Angela
Rudi Angela

Reputation: 1473

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

[config setHTTPAdditionalHeaders:@{@"Authorization":@"token=\"4959a0bc00a15e335fb6\""}];

Upvotes: 1

Related Questions