gpt4 book ai didi

ios - AFHTTPSessionManager header

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:08:49 25 4
gpt4 key购买 nike

我正在尝试通过设置 HTTPAdditionalHeaders 为“Content-Type”设置默认 header 。当我查看请求 header 时,AFNetworking (v 2.0.3) 将其改回。我还尝试通过 setValue:forHTTPHeaderField: 在 requestSerializer 上设置 header ,但没有成功。我缺少什么?

已更新

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

最佳答案

我认为 AFNetworking 会自动设置 Content-Type,您无法更改它。使用 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) {

}];

关于ios - AFHTTPSessionManager header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20686179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com