AFNetworking multipart/form-data POST 发送不正确的 JSON 字典数组

AFNetworking multipart/form-data POST sends incorrect JSON array of dictionaries

我不知道这是一个错误还是我没有做对。

这个 post 看起来很接近为我解决它但没有:AFNetworking posts JSON arrays as multiple single-entry dictionaries

我正在尝试发送带有图像的 multipart/form-data POST 并将字典(包含字典数组)传递给 'parameters' 以作为 [=38] 发送=].

词典:

NSDictionary *parameters = @{@"photos" : @[@{@"photoID":@"0", @"imageURL":@"0", @"imageName":@"0"},
                                           @{@"photoID":@"1", @"imageURL":@"1", @"imageName":@"1"},
                                           @{@"photoID":@"2", @"imageURL":@"2", @"imageName":@"2"}]};

要求:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];


[manager POST:@"myURL" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        [formData appendPartWithFileData:imageData name:@"image" fileName:@"image" mimeType:@"image/png"];

    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Success: %@", responseObject);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

图像发送和接收正常,但是服务器上收到的 JSON 看起来像这样:

{
    photos =     (
                {
            imageName = 0;
        },
                {
            imageURL = 0;
        },
                {
            photoID = 0;
        },
                {
            imageName = 1;
        },
                {
            imageURL = 1;
        },
                {
            photoID = 1;
        },
                {
            imageName = 2;
        },
                {
            imageURL = 2;
        },
                {
            photoID = 2;
        }
    );
}

...多个单条目字典的数组:(

而不是:

{
    photos =     (
                {
            imageName = 0;
            imageURL = 0;
            photoID = 0;
        },
                {
            imageName = 1;
            imageURL = 1;
            photoID = 1;
        },
                {
            imageName = 2;
            imageURL = 2;
            photoID = 2;
        }
    );
}

任何帮助都会很棒,谢谢!

AFNetworking 中构造多部分请求时,参数作为 form-data 发送。要在您的多部分响应中发送 JSON 编码正文,您需要将它们序列化并将它们附加到多部分响应中,作为您的服务器期望接收它们的方式。

根据您的服务器对它们的期望,您可以使用如下内容:

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myJSON options:0 error:nil];

 [formData appendPartWithFileData:jsonData name:@"name" fileName:nil mimeType:@"application/json"];

如果您想上传多张图片,并且希望所有图片的参数名称保持相同,请按以下步骤操作:

NSDictionary *parameters = @{@"user_key": @"*****"};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:queryStringss parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
 NSError *error;
[formData appendPartWithFileData:imageData name:@"photo_file[0]" fileName:@"Picture44.png" mimeType:@"image/png"];
[formData appendPartWithFileData:imageData1 name:@"photo_file[1]" fileName:@"Picture45.png" mimeType:@"image/png"];
} error:nil];

这样,文件将作为数组发送到服务器。