Search code examples
phpiosobjective-cafnetworkingafnetworking-2

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


I don't know if this is a bug or I'm just not doing it right.

This post looked so close to solving it for me but didn't: AFNetworking posts JSON arrays as multiple single-entry dictionaries

I'm trying to send a multipart/form-data POST with an image and passing a dictionary (which contains an array of dictionaries) to 'parameters' to be sent as JSON.

The dictionary:

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

The request:

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

The image is being sent and received fine, however the JSON received on the server looks like this:

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

...an array of multiple single-entry dictionaries :(

Instead of:

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

Any help would be great, thanks!


Solution

  • Parameters are sent as form-data when constructing a multi-part request in AFNetworking. To sent JSON encoded bodies inside your multi-part response, you will need to serialize them and append them to the multi-part response as how your server expects to receive them.

    Depending on how your server is expecting them, you could use something like:

     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:myJSON options:0 error:nil];
    
     [formData appendPartWithFileData:jsonData name:@"name" fileName:nil mimeType:@"application/json"];