5

Does anybody know where an iOS app can see the default headers that NSUrlRequest sets for an HTTP request?

Just creating NSUrlRequest with "http://.." NSURL and then asking: [request allHTTPHeaderFields] returns an empty dictionary. But I know that for example "Accept-Encoding" is set to "gzip". So I want to get all that fields and show them in a HTTP request demo.

I've also tried to swizzle [NSMutableURLRequest setValue:forHTTPHeaderField:], but it seems that it is not used by underlying API (NSURLRequest or NSURLConnection) to set those default fields I'm hunting for.

I'm making just a simple iOS demo which shows HTTP request and response information, so it doesn't really matters if it will be a public or private API used for that.

3 Answers 3

14

Your app cannot. It's done all down in CFNetwork - Communicating with HTTP Servers. I believe it just adds missing header values not supplied by NSURLRequest.

The defaults are:

  • USER-AGENT "AppName - Eng/1.0 CFNetwork/485.13.9 Darwin/10.7.0"
  • ACCEPT "*/*"
  • ACCEPT-LANGUAGE "en-us"
  • ACCEPT-ENCODING "gzip, deflate"
  • CONNECTION "keep-alive"
1
  • 1
    There is also HOST "host:port";
    – zubko
    Apr 22, 2011 at 1:18
5

hmm... maybe you might want to try within

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse

method in your custom nsurlconnection class. although the documentation mentions something about redirects, this is certainly worth looking into.

2
  • Nice shot, but unfortunately not 100% hit.
    – zubko
    Apr 20, 2011 at 20:31
  • 1
    Indeed -(NSURLRequest *)connection:willSendRequest: redirectResponse: will be called for a simple request when no redirection is needed (despite official docs) and its header will have some fields at this stage: { Accept = "*/*"; Accept-Encoding" = "gzip, deflate"; Accept-Language" = "en-us";} but there more: "Host", "User-Agent" and "Connection" which are set at some lower level - I've run a simple local HTTP server to see all of them.
    – zubko
    Apr 20, 2011 at 20:40
2

That may be an overkill, but based on Matt Gallagher's blog post I've created even more simpler local HTTP listener and sent a separate [mutableCopy]ed request to it to be able to read and output all HTTP headers that this request has.

Better solution must be to setup a local HTTP request catcher, it must look nicer I think, but for the scope of simple demo just to show all sent/received headers this solution is OK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.