I'm trying to add a value to the header for a URL request.
Something like this works just fine:
[urlRequest addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
But this doesn't even show up in the header:
NSString *authString = [[NSString alloc] initWithString:
[defaults objectForKey:@"auth"]];
[urlRequest addValue:authString forHTTPHeaderField:@"iphoneID"];
I'm completely stumped. The auth string is around 90 characters long. Is this a problem?
Edit: Here's the code I'm trying:
NSString *authString = [[NSString alloc] initWithString:[defaults objectForKey:@"auth"]];
[urlRequest addValue:authString forHTTPHeaderField:@"iphoneid"];
[urlRequest addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
I can see the Accept-Encoding header being sent through Wireshark, but iphoneid is nowhere to be found. It's just a string, 80-90 characters long.
Another Update: So it seems that the problem isn't the field "iphoneid" but rather the authString I'm trying to pass into it. Other strings that I just create with the @"something" work fine, but the auth string that I pull from NSUserDefaults doesn't appear.
Suggestions on how to debug this?
The string I was pulling from NSUserDefaults already had a line ending. When set as a header, another \r\n
is appended, which apparently isn't valid. Thus, the header wouldn't appear in any outgoing packets.
Use this to trim off the characters before setting as a header value.
[yourString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
Testing checklist:
Verify that you actually have a NSMutableURLRequest (and not a NSURLRequest) at this point. In particular, check your logs for an exception due to "unrecognized selector."
Verify that urlRequest is not nil.
Switch to setValue:forHTTPHeaderField: rather than addValue:forHTTPHeaderField:.
Swap the forHTTPHeaderField: value to @"Accept-Encoding" to see if the field is the problem
Swap @"gzip" for auth to see if the value is the problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With