0

How to send a nested json request which includes an image in an array of dictionary ex:

    @{"Contact" : @[@{
                "id" : "x1",
                "name" : "my product1",
                "image" : #<image1>
               },
               {
                "id" : "x2",
                "name" : "my product2", 
                "image" : #<image2>
              }] 
            }; 

We are taking reference from this link AFNetworking post image in nested json

Community
  • 1
  • 1
VSP
  • 166
  • 11

1 Answers1

0

try this

  NSData *imgData = UIImagePNGRepresentation(anImage);
  NSString *newStr = [anImageName stringByReplacingOccurrencesOfString:@"/"
                                                          withString:@"_"];



  [request addData:imgData
   withFileName:[NSString stringWithFormat:@"%@.png",newStr]
   andContentType:@"image/png"
   forKey:anOtherKey];

I digged into AFNetworking documentation and found they appending the image in an NSMutableRequest like this:

  AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
  NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);
  NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
 [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];
 }];
soumya
  • 3,801
  • 9
  • 35
  • 69
  • Hi …. Thanks for reply. Here, we are passing contacts[image] to indicate that we have dictionary inside another dictionary like {"Contact" : @{ "id" : "x1", "image" : # } } [formData appendPartWithFileData:pictureData name:@"contacts[image]" fileName:@"image" mimeType:@"image/png"]; But we are having an array of dictionaries like @{"Contact" : @[@{ "id" : "x1","image" : # }] }; Now what we have to pass in place of contacts[image] ? – VSP Aug 06 '15 at 06:46