AFHTTPSessionManager header

强颜欢笑 提交于 2019-12-21 06:06:38

问题


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.



来源:https://stackoverflow.com/questions/20686179/afhttpsessionmanager-header

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!