views:

998

answers:

2

I am having issues with adding a header to NSMutableURlRequest, the problem is that when I add the header below "Authorization" it does not show up. However if I replace my instance variable "auth" below with a static string exp (@"asdadsadsadga") it the "Authorization" header will show up. I am pretty lost at this point.

NSURL *url = [NSURL URLWithString:@"https://www.google.com/analytics/feeds/accounts/default"];
NSMutableURLRequest *profileRequest = [NSMutableURLRequest requestWithURL:url];

NSLog(auth); //prints correctly
NSString *authString = [NSString stringWithFormat:@"GoogleLogin Auth=%@", auth];
[profileRequest addValue:authString forHTTPHeaderField:@"Authorization"];

NSDictionary *allheaders = [profileRequest allHTTPHeaderFields];

for (id key in allheaders)
{
  //Nothing prints
  NSLog(@"key: %@, value: %@", key, [allheaders objectForKey:key]);
}

NSLog(auth); //Prints correctly
+1  A: 

First, swap out NSLog(auth) with NSLog(@"%@", auth). The former is dangerous, and may expose some of what's going on here. You should also try adding early on:

auth = @"asdasdasdad";

You should also try just dumping allheaders directly:

NSLog(@"%@", [profileRequest allHTTPHeaderFields];

This may shed some light. I suspect that the problem is in your auth variable, possibly related to memory management, not in NSMutableURLRequest.

Rob Napier
While the NSLog statement is potentially crashy/incorrect, the creation of authString is safe, so it's hard to see how that could be relevant.It also can't be a memory management issue, since according to the sample code auth is still valid at the end of the chunk of code that doesn't work.
smorgan
Your problem isn't reproducible, so the actual content of "auth" is extremely relevant.
Azeem.Butt
Auctually, I seem to have reproduced it. It must be with how we are getting the string. I am using some code I found to base64Encode the username/password, and clearly it is failing.
Matthew Schinckel
A: 

The code you are using to generate your base64Encoded string is broken.

Try using the method outlined at the bottom of the page: http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

Matthew Schinckel