AFNetworking在嵌套json中发布图像 [英] AFNetworking post image in nested json

查看:112
本文介绍了AFNetworking在嵌套json中发布图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须发送一个嵌套的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>
  } 
}

问题是如果我尝试使用 multipartFormRequestWithMethod:path:parameters:constructBodyWithBlock:(和 appendPartWithFileData:name:fileName:mimeType:),传入catalogue_id并命名为params,图像字段附加在产品之后,如下所示:

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?

谢谢堆

推荐答案

通过一些修补找到答案:产品[image] 在名称中诀窍

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

示例代码:

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

这篇关于AFNetworking在嵌套json中发布图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆