AFHTTPSessionManager header

2019-03-31 06:40发布

问题:

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

回答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) {

}];


回答2:

AFNetworking comes with a AFJSONRequestSerializer and AFJSONResponseSerializer:

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


回答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.