NSURLRequest setting the HTTP header

Raphael picture Raphael · Jan 26, 2011 · Viewed 80.6k times · Source

I need to set the HTTP header for a request. In the documentation for the NSURLRequest class I didn't find anything regarding the HTTP header. How can I set the HTTP header to contain custom data?

Answer

Larry Hipp picture Larry Hipp · Jan 26, 2011

You need to use a NSMutableURLRequest

NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url]
                                autorelease];

[request setValue:VALUE forHTTPHeaderField:@"Field You Want To Set"];

or to add a header:

[request addValue:VALUE forHTTPHeaderField:@"Field You Want To Set"];