AFNetworking post image in nested json

戏子无情 提交于 2019-12-18 05:13:10

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/11087678/afnetworking-post-image-in-nested-json

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!