ioshttpwebrequestafnetworking-2

AFNetworking - Request format is not proper when using dictionaries


I'm sending a dictionary 'childDetails' which has a dictionary (rewards) as one of its objects.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:url parameters:childDetails constructingBodyWithBlock:^(id<AFMultipartFormData> formData) 

This "rewards" dictionary has two keys "name" and "value" which looks like {@"name",@"Reward 1",@"value",@"10"}.

when this gets posted to the server, the server receives it as follows;

 Array
(
    [group_id] => 5
    [name] => John Doe
    [rewards] => Array
        (
            [0] => Array
                (
                    [name] => Sample reward 2
                )

            [1] => Array
                (
                    [value] => 50
                )

            [2] => Array
                (
                    [name] => Sample Reward 1
                )

            [3] => Array
                (
                    [value] => 10
                )

        )

    [tasks] => Array
        (
            [0] => Array
                (
                    [title] => Default task one
                )

            [1] => Array
                (
                    [title] => Default task two
                )

            [2] => Array
                (
                    [title] => Default task five
                )

        )

    [token] => 5332884c2bc8c5
)

Any Idea how to fix this?

Any help is much appreciated.

Thanks in advance


Solution

  • I found out that inorder to get it to work, you have to reformat your dictionary. In my case I had to change it as;

    NSDictionary *parameters = @{
        @"rewards": @[
            {@"reward name",@"reward value"},
            {@"reward name",@"reward value"}
        ]
    };;