Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HTTP Header to NSURLRequest

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

[request addValue:@"PC" forHTTPHeaderField:@"machineName"]
like image 248
Ahmad Kayyali Avatar asked Nov 24 '10 09:11

Ahmad Kayyali


People also ask

How do I add HTTP headers?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

What is HTTP header in Java?

An HTTP header is a field of an HTTP request or response that passes additional context and metadata about the request or response.


3 Answers

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); 
like image 66
Hless Avatar answered Nov 05 '22 14:11

Hless


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"]; 
like image 43
Ferran Maylinch Avatar answered Nov 05 '22 12:11

Ferran Maylinch


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];
like image 24
Zubair Avatar answered Nov 05 '22 12:11

Zubair