Add HTTP Header to NSURLRequest
Asked Answered
M

3

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"]
Mckenzie answered 24/11, 2010 at 9:18 Comment(0)
E
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);
Esteresterase answered 24/11, 2010 at 9:32 Comment(5)
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.Mckenzie
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.Esteresterase
hey @Esteresterase I tried this method but I'm still getting unauthorized. Can you see my question here: #40343228Snicker
Or just create an NSMutableRequest from the start because you expect to add a header.Prem
@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.Esteresterase
C
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"];
Camphene answered 1/7, 2015 at 14:8 Comment(1)
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.Esteresterase
A
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];
Apophasis answered 12/7, 2012 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.