How to set Http header Fields in Objective-C?
You can pass the information in header using NSMutableURLRequest class and then call the NSURLConnection class(it will call the connection delegate).
see the following code,
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:[myServerUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
//do post request for parameter passing
[theRequest setHTTPMethod:@"POST"];
//set the content type to JSON
[theRequest setValue:@"xml" forHTTPHeaderField:@"Content-Type"];
//passing key as a http header request
[theRequest addValue:@"value1" forHTTPHeaderField:@"key1"];
//passing key as a http header request
[theRequest addValue:@"value2" forHTTPHeaderField:@"key2"];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
webData = [[NSMutableData data] retain];
}
else
{
NSLog(@"theConnection is NULL");
}
[theConnection release];
How to set http header for for HTTP in Objective-C?
Use the following:
[request setValue:@"xxx" forHTTPHeaderField:@"Content-Type"];
But you have to change the NSURLRequest
to NSMutableURLRequest
, please check Zaph's answer
NSURLRequest setting the HTTP header
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"];
How to get HTTP headers
This falls under the easy, but not obvious class of iPhone programming problems. Worthy of a quick post:
The headers for an HTTP connection are included in the NSHTTPURLResponse
class. If you have an NSHTTPURLResponse
variable you can easily get the headers out as a NSDictionary
by sending the allHeaderFields message.
For synchronous requests — not recommended, because they block — it’s easy to populate an NSHTTPURLResponse
:
NSURL *url = [NSURL URLWithString:@"http://www.mobileorchard.com"];
NSURLRequest *request = [NSURLRequest requestWithURL: url];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest: request returningResponse: &response error: nil];
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [response allHeaderFields];
NSLog([dictionary description]);
}
With an asynchronous request you have to do a little more work. When the callback connection:didReceiveResponse:
is called, it is passed an NSURLResponse
as the second parameter. You can cast it to an NSHTTPURLResponse
like so:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog([dictionary description]);
}
}
How do i set multiple http header using sessionManager (AFNetworking)
The Accept
header takes a comma separated list, so something like this should work:
[_sessionManager.requestSerializer setValue:@"application/json, application/xml" forHTTPHeaderField:@"Accept"];
Obviously replace application/xml
with whatever you need.
Related Topics
Can't Load Uiviewcontroller Xib File in Storyboard in Swift
Get Instance of Viewcontroller from Appdelegate in Swift
How to Draw a Point Using Core Graphics
Determine User's "Temperature Unit" Setting on iOS 10 (Celsius/Fahrenheit)
Is There a Simple Way to Edit/Modify a Uilocalnotification
Increment Uislider by 1 in Range 1 to 100
Urlresponse Is Not Retrieved After Storing in Cache Using Storecachedresponse
How to Make Generics in Collection Type Constraint
What Does the Property "Nonatomic" Mean
Nsuserdefaults Losing Its Keys & Values When Phone Is Rebooted But Not Unlocked
Codesign Returned Unknown Error -1=Ffffffffffffffff
What Is Difference Between Urlwithstring and Fileurlwithpath of Nsurl
Perform a Deeplink from Swiftui Widget on Tap
Flutter on iOS: Fatal Error: Module 'Cloud_Firestore' Not Found