52

Is there any way to add HTTP header to NSURLRequest object? I used to add them in NSMutableURLRequest using:

[request addValue:@"PC" forHTTPHeaderField:@"machineName"]
Termininja
  • 6,620
  • 12
  • 48
  • 49
Ahmad Kayyali
  • 8,233
  • 13
  • 49
  • 83

3 Answers3

81

I don't think you can modify the HTTP Headers of a NSURLRequest. I think you're trying to modify a NSURLRequest object that you didn't initialize?

You could create a mutableCopy of your request and then set the header fields with the following method:

 -(void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field.

After that you can copy the mutable request back onto your NSURLRequest variable.

EDIT: Added example below

/* Create request variable containing our immutable request
 * This could also be a paramter of your method */
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];

// Create a mutable copy of the immutable request and add more headers
NSMutableURLRequest *mutableRequest = [request mutableCopy];
[mutableRequest addValue:@"Hless" forHTTPHeaderField:@"X-user-nick"];

// Now set our request variable with an (immutable) copy of the altered request
request = [mutableRequest copy];

// Log the output to make sure our new headers are there    
NSLog(@"%@", request.allHTTPHeaderFields);
Hless
  • 3,326
  • 20
  • 22
  • 1
    Thanks for the response, the thing is you can't add Http Headers to the NSURLRequest you can only get them, to be honest i wanted to use NSURLRequest because i wanted to receive a chunked file using the the delegate "didReceiveData" but now i am using threads instead with NSMutableURLRequest. – Ahmad Kayyali Nov 24 '10 at 10:12
  • I know it's been a while since I posted this, but due to somewhat popular demand I updated the answer for correctness and added an example. – Hless Sep 03 '13 at 10:47
  • hey @Hless I tried this method but I'm still getting unauthorized. Can you see my question here: http://stackoverflow.com/questions/40342728/sending-httpheaderfield-for-nsurlrequest-in-afmultipartformdata – Chaudhry Talha Oct 31 '16 at 14:05
  • Or just create an NSMutableRequest from the start because you expect to add a header. – Alex Zavatone Jul 15 '19 at 20:26
  • @AlexZavatone Based on the question and OP's comment I'm guessing he recieved a NSURLRequest from another library or so. So in that particular case he wouldn't be able to use a NSMutableRequest from the start. He also states in his question that he knows how NSMutableRequest works. – Hless Jul 16 '19 at 08:43
9

Already answered (and thanks to those answers), but here's a more simple example:

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setValue:@"es" forHTTPHeaderField:@"Accept-Language"];
Ferran Maylinch
  • 10,919
  • 16
  • 85
  • 100
  • If you read the question it's actually not. OP states that he knows how a NSMutableURLRequest works. If you want to modify an existing NSURLRequest that you did not initialize in your own code (e.g. from a library), you will have to use the method in my answer, or you'll lose the headers etc from the original request. – Hless Jul 16 '19 at 08:42
5
NSString *urlString = @"http://10.28.79.63:3000/testing";

NSMutableURLRequest *imageRequest = [[NSMutableURLRequest alloc] init] ;
[imageRequest setURL:[NSURL URLWithString:urlString]];
[imageRequest setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];

[imageRequest setValue:contentType forHTTPHeaderField: @"content-Type"]; 
NSMutableData *body = [NSMutableData dataWithCapacity:[imageData length] + 512];
[body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary]dataUsingEncoding:NSUTF8StringEncoding]]; 

//Here u adds the Headers which will be posted in body 

[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; content-type: application/octet-stream; name=\"userfile\"; filename=\"inLove.mp4\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[imageRequest setHTTPBody:body];
NSURLConnection * theConnection = [[NSURLConnection alloc] initWithRequest:imageRequest 
                                                delegate:self];
nmr
  • 16,625
  • 10
  • 53
  • 67
Zubair
  • 5,833
  • 3
  • 27
  • 49