我正在开发一个具有更新API
作为PUT
方法的应用,该方法将多个对象的数组作为批量更新的参数。
这是params格式:
{
1 = {
"badge_number" = 20;
email = "test@gmail.com";
"event_id" = 120;
"participant_id" = 129333;
"image" = <ffd8ffe0 00104a46 49460001...>;
"name" = "Badge for Event 120";
"version" = 167;
},
2 = {
"badge_number" = 20;
email = "test@gmail.com";
"event_id" = 120;
"participant_id" = 129333;
"image" = <ffd8ffe0 00104a46 49460001...>;
"name" = "Badge for Event 120";
"version" = 167;
}...;
}
以下是我如何构造请求主体以包含要使用multipart/form-data
为图像发送的数组参数中的每个对象的图像。我之前使用的是x-www-form-urlencoded
,但看起来api没有从图像的NSData
获取64个基本编码的字符串。
NSMutableURLRequest *request = [httpRequestManager.requestSerializer multipartFormRequestWithMethod:@"PUT" URLString:[NSString stringWithFormat:@"%@%@",BASE_URL,@"update"] parameters:batch constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (NSString *order in [batch allKeys]) {
[formData appendPartWithFileData:participantBatch[order][@"image"] name:[NSString stringWithFormat:@"%@[%@]",order,@"update_image"] fileName:[NSString stringWithFormat:@"%@[%@].jpg",order,@"update_image"] mimeType:@"image/jpeg"];
}
} error:nil];
NSLog(@"Request body %@", [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]);
[[manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
[subject sendNext:responseObject];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[subject sendError:error];
}] start];
由于某种原因,请求HTTPBody
始终返回nil
。我AFNetworkActivityLogger
上有debug level
,但它只显示请求和响应的标头字段。有什么想法吗?