djibouti33
djibouti33

Reputation: 12132

Posting Image with array parameters using AFNetworking 2.0 not working

I'm trying to post an image with a parameter (categories) that is an array. If I leave this parameter out, I can submit fine. However, if I leave it in, my server throws an error because the array value doesn't seem to be getting serialized correctly.

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:@{
    @"auth_token" : authToken,
    @"title" : title,
    @"categories" : @[1,2,3]
}];

return [[MyHTTPSessionManager sharedManager] POST:path parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"image"
                            fileName:@"image.jpg"
                            mimeType:@"image/jpeg"];
} success:nil failure:nil];

Looking at my server logs, this is how categories is coming across:

Parameters: {"categories"=>"(\n    1,\n    2,\n    3\n)", … }

And I need it to look like:

Parameters: {"categories"=>[1,2,3], … }

I've tried setting my instance of AFHTTPSessionManager's requestSerializer to [AFJSONRequestSerializer serializer] but that doesn't seem to help.

How can I properly POST an image + parameters? I'd like to avoid falling back to AFHTTPRequestOperation.

Thanks!

Upvotes: 0

Views: 678

Answers (1)

Aaron Brager
Aaron Brager

Reputation: 66302

Try upgrading to the newest version of AFNetworking. I believe this was fixed in github.com/AFNetworking/AFNetworking/issues/1388, which I think was rolled into AFNetworking 2.0.1. If the newest version doesn't work, just pull the latest commit.

Upvotes: 1

Related Questions