AFNetworking PUT构造多个/表单数据的主体,如何记录发送到API的主体内容/参数?

时间:2015-04-20 22:31:22

标签: ios objective-c api afnetworking afnetworking-2

我正在开发一个具有更新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,但它只显示请求和响应的标头字段。有什么想法吗?

0 个答案:

没有答案