gpt4 book ai didi

ios - AFnetworking 多部分请求正文为零

转载 作者:可可西里 更新时间:2023-11-01 06:17:19 25 4
gpt4 key购买 nike

我正在尝试通过 af 2.0 多部分请求将图片上传到服务器,但似乎正文始终为空。这是代码的快照:

[self POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:file name:@"file"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
andAPIResponseType:apiResponseType];
} failure:^(NSURLSessionDataTask *task, NSError *error) {

}];

其中文件是我从设备相机拍摄的照片,并做了类似的事情

 NSData *file = UIImageJPEGRepresentation(avatar, 0.5)

然而,当我记录我通过 AF 记录器发送的内容时,我得到这样的结果

103762376897069058/avatar?access_token=4f77072f0f7de259293f229c7a876130c79705d7': {
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5";
"Content-Length" = 29120;
"Content-Type" = "multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY";
"User-Agent" = "MixHR/0.0.1 (iPhone; iOS 7.0.4; Scale/2.00)";
} (null)

那么为什么 body 为零?我试过记录文件,它不是空的。

最佳答案

你试试这段代码:

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *strURL = @"http://YOUR_IP:YOUR_PORT/YOUR_PATH/..";

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializerRequest = [AFJSONRequestSerializer serializer];
[serializerRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

AFJSONResponseSerializer *serializerResponse = [AFJSONResponseSerializer serializer];
serializerResponse.readingOptions = NSJSONReadingAllowFragments;
serializerResponse.acceptableContentTypes = [NSSet setWithObject:@"application/json"];

manager.requestSerializer = serializerRequest;
manager.responseSerializer = serializerResponse;

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];

NSDictionary *parameters = @{@"name":@"xxxx",
@"surname":@"xxxx",
};
[manager POST:strURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFormData:imageData name:@"photo"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@""%@,operation.responseString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@""%@,operation.responseString);
}];
}

关于ios - AFnetworking 多部分请求正文为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20774692/

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