Issues with NSURLSession Not Sending HTTPAdditionalHeaders

1.7k Views Asked by At

I can't seem to get my networking code to work. I've spent at least a couple hours looking through documentation and Google this over the last 2 days and I can't figure it out. For whatever reason, it appears my NSURLSession isn't sending along the headers. I preformed the proper request in Postman for Chrome and am using those exact parameters so I know I'm not sending incorrect data. I continue to get an error that I was able to reproduce in Postman by removing header parameters.

Code:

NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];

NSDictionary *sessionHeaders = [[NSDictionary alloc] initWithObjectsAndKeys:
@"application/json", @"Accept", @"application/xml", @"Content-Type", 
[NSString stringWithFormat:@"LivePerson appKey=%@", self.getAppKey], @"Authorization", nil];

sessionConfig.HTTPAdditionalHeaders = sessionHeaders;

NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];

NSURLSessionDataTask *endpoints = [session dataTaskWithURL:[NSURL URLWithString:baseURL] 
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

NSDictionary *endpoints = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"%@", endpoints);
}];
1

There are 1 best solutions below

0
On

It's the other way round... this should do it

NSDictionary *sessionHeaders = [[NSDictionary alloc] initWithObjectsAndKeys:
@"Accept", @"application/json", @"Content-Type", @"application/xml",  
@"Authorization", [NSString stringWithFormat:@"LivePerson appKey=%@", self.getAppKey], nil];