AFHTTPSessionManager header

12,914

Solution 1

I think that AFNetworking set Content-Type automatically and you can not change it. To send data using Content-Type multipart/form-data:

NSURL *URL = [NSURL URLWithString:@"http://example.com/api"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

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 constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    //If you need to send image
    UIImage *image = [UIImage imageNamed:@"my_image.jpg"];
    [formData appendPartWithFileData:UIImageJPEGRepresentation(image, 0.5) name:@"Image" fileName:@"my_image.jpg" mimeType:@"image/jpeg"];

} success:^(NSURLSessionDataTask *task, id responseObject) {

} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

Solution 2

AFNetworking comes with a AFJSONRequestSerializer and AFJSONResponseSerializer:

[manager setRequestSerializer:[[AFJSONRequestSerializer alloc] init]];
[manager setResponseSerializer:[[AFJSONResponseSerializer alloc] init]];

Solution 3

In the AFURLRequestSerialization.m file you can find the following property:

@property (readwrite, nonatomic, strong) NSMutableDictionary *mutableHTTPRequestHeaders;

Now you can subclass AFHTTPRequestSerializer (or AFJSONRequestSerializer) and add your desired HTTP headers to that mutable dictionary (don't forget to import the AFURLRequestSerialization.m file in your request serializer .m file).

Then you just set the requestSerializer property of your AFHTTPSessionManager subclass to a new object of your new request serializer class (e.g. in the init method) and you're done. All requests with your session manager should include your HTTP headers then.

Share:
12,914
sash
Author by

sash

Updated on June 04, 2022

Comments

  • sash
    sash almost 2 years

    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");
        }];