Saheb Singh Saheb Singh - 1 year ago 58
Objective-C Question

How do i set multiple http header using sessionManager (AFNetworking)

I have searched through various stackoverflow questions and I got answer but using

as there is one method
- (void)addValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
which can be called over
How do I add value using
?Or any work around. Here is the code which I am using for session configuration.

- (void)configureSesionManager {
_sessionManager = [AFHTTPSessionManager manager];
_sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];

_sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
_sessionManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
_sessionManager.requestSerializer.timeoutInterval = 360;
if ([GSCommonUtil isUserLoggedIn]) {
authenticationHeader = [NSString stringWithFormat:@"Bearer %@",[GSCommonUtil retriveValueFromUserDefaults:kNSUserAuthenticationToken]];
[_sessionManager.requestSerializer setValue:authenticationHeader forHTTPHeaderField:@"Authorization"];
[_sessionManager.requestSerializer setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
[_sessionManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
// I have to add one more HTTPHeader for @"Accept". How could I achieve it.
NSLog(@"Done with Session Manager Configuration!");

Answer Source

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.