views:

3282

answers:

3

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?

A: 

You need Charles web proxy, to see what header values are really outbound and inbound. Then you can see if the problem is really in your code or some magic on the server discarding things.

There's a free trial, and after you install it if you hit record any traffic the simulator sends will go through the proxy. Very nice.

http://www.charlesproxy.com/

Kendall Helmstetter Gelner
I've been using Wireshark that does total packet capture over network interfaces and let's me follow a TCP stream. Is there something unique about Charles?
Michael Grinich
Just easier to use and view results from in different ways.If you are happy with Wireshark that is fine, as long as you can see the data you need.
Kendall Helmstetter Gelner
+1  A: 

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.

Rob Napier
+5  A: 

The true problem.

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.

The fix:

Use this to trim off the characters before setting as a header value.

[yourString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];

Michael Grinich