I am trying to set a default header for "Content-Type" by setting HTTPAdditionalHeaders. When I look at the request header, AFNetworking (v 2.0.3) changes it back. I also tried to set header by setValue:forHTTPHeaderField: on the requestSerializer, but no success. What I am missing?
UPDATED
NSURL *URL = [NSURL URLWithString:@"http://example.com/api"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.HTTPAdditionalHeaders = @{@"Content-Type": @"multipart/form-data"};
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:URL sessionConfiguration:configuration];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
[params setValue:@"some value" forKey:@"someKey"];
[manager POST:@"search" parameters:params success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"success");
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"error");
}];
In the
AFURLRequestSerialization.m
file you can find the following property:Now you can subclass
AFHTTPRequestSerializer
(orAFJSONRequestSerializer
) and add your desired HTTP headers to that mutable dictionary (don't forget to import theAFURLRequestSerialization.m
file in your request serializer .m file).Then you just set the
requestSerializer
property of yourAFHTTPSessionManager
subclass to a new object of your new request serializer class (e.g. in theinit
method) and you're done. All requests with your session manager should include your HTTP headers then.AFNetworking comes with a
AFJSONRequestSerializer
andAFJSONResponseSerializer
:I think that AFNetworking set Content-Type automatically and you can not change it. To send data using Content-Type multipart/form-data: