Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AFNetworking post image in nested json

I have to send a nested json request which includes an image in an inner hierarchy. eg:

{"product" : {
  "catalogue_id" : "x", 
   "name" : "my product", 
   "image" : #<image>
  } 
}

Problem is if I try using multipartFormRequestWithMethod:path:parameters:constructingBodyWithBlock: (and appendPartWithFileData:name:fileName:mimeType:), passing in catalogue_id and name as params, the image field is appended after "product", like so:

{"product" : {
  "catalogue_id" : "x", 
   "name" : "my product"
  } ,
   "image" : #<image>
}

Is there a way to specify that the image field is nested at certain depth?

Thanks heaps

like image 662
Nav Avatar asked Aug 06 '26 12:08

Nav


1 Answers

Found the answer with some tinkering: product[image] in the name did the trick

Sample code:

NSMutableURLRequest *request = [[client sharedInstance]
                         multipartFormRequestWithMethod:@"POST" 
                                                   path:@"/catalogues/1/products.json" 
                                             parameters:params
                              constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                  [formData appendPartWithFileData:img
                                                              name:@"product[image]" 
                                                          fileName:@"myimage.jpg" 
                                                          mimeType:@"image/jpg"];
                                  }];
like image 151
Nav Avatar answered Aug 08 '26 01:08

Nav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!