Objective-C: objective-c response differs from curl response with similar http request

54 views Asked by At

Using below curl command I am able to get a valid response with required token.

curl -v -h "TOKEN:tokenValue" @“someurl.com” 

However, When I try to create a NSMutableURLRequest as shown below, I get a different response.

NSURL* url = [NSURL URLWithString:kURL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest addValue:tokenValueStr forHTTPHeaderField:@"TOKEN"];
[urlRequest setHTTPMethod:@"GET"];
[NSURLConnection sendAsynchronousRequest:urlRequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:handler];

I have checked the [request allHTTPHeaderfields] and it shows dictionary with TOKEN key-value pair. and Token and url are valid URLs. Theoretically this request is what I am supposed to get the right response but it isnt and I did not find any useful reference online either. Any suggestions are appreciated.

1

There are 1 answers

4
Dale Myers On

When you pass a header into curl, you must give the name and value of the header like this:

HeaderName:HeaderValue

For the Objective-C parameter, you are passing in a value and a name. The value should just be "HeaderValue" and the name would be "HeaderName", which is "TOKEN" in this case.