Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Patrick Cooney
12,216 PointsIssues with NSURLSession Not Sending HTTPAdditionalHeaders
I can't seem to get my networking code to work. I've spent at least a couple hours looking through documentation and googling 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 pre-formed 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.
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);
}];
Hopefully someone can help me out. I'm trying to get the requests ironed out before WWDC so I can get to the important stuff so I can ask questions I come across in labs. Amit Bijlani or Sam Soffes?
3 Answers

Amit Bijlani
Treehouse Guest TeacherI would try creating a mutable request and adding you session header information there. See this post as an example: http://stackoverflow.com/a/19101084

Thomas Nilsen
14,957 PointsHaving a look in the documentation at the NSURLSessionConfiguration. Under 'HTTPAdditionalHeaders' it says: 'A dictionary of additional headers to send with requests'. So my first guess is (sorry if it's not a good one), change the
NSURLSessionDataTask *endpoints = [session dataTaskWithURL:[NSURL URLWithString:baseURL] completionHandler:^(NSData *data,
}];
to:
NSURLSessionDataTask * endpoints = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
}];

Patrick Cooney
12,216 PointsThanks guys. It still wasn't working, luckily after going back into the documentation for the REST API I'm using, I found that the appKey
can be sent as a parameter in the request url in place of an Authorization
header. I'm not sure why that header wasn't working because it definitely is honoring my Accept
and Content-Type
headers.
Eric Peterson
189 PointsEric Peterson
189 PointsThis was the approach that solved the problem for me. Config'ing the session didn't work as doc'd or expected in a number of ways.