Restkit:multipartFormRequestWithObject不是json [英] Restkit : multipartFormRequestWithObject not json

查看:118
本文介绍了Restkit:multipartFormRequestWithObject不是json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RestKit 2.0通过'multipartFormRequestWithObject'方法将核心数据实体和图像发送到服务器.但是,当实体数据到达时,它不是json格式.如果我使用"postObject"发送没有图像的实体,则数据为json格式.在两种情况下,我都使用相同的RKObjectMapping. 我要怎么做才能将该对象序列化为json?我尝试过

I am using RestKit 2.0 to send a core data entity and an image to a server with the 'multipartFormRequestWithObject' method. However, when the entity data arrives it is not in json format. If I send the entity using 'postObject' without an image then the data is in json format. I use the same RKObjectMapping for both situations. What do I have to do to make the Object serialize to json? I tried

[request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; 

但这没有帮助,我已经有了对象管理器的设置,

But that didn't help and I already have my object manager settings as so:

[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];

我的标题内容类型是multipart/form-data,但是我想这是必需的.

My Header Content-Type is multipart/form-data but I guess that is required.

request.headers={
Accept = "application/json";
"Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
"Accept-Version" = 1;
"Content-Length" = 19014;
"Content-Type" = "multipart/form-data; boundary=Boundary+0xAbCdEfGbOuNdArY";
"User-Agent" = "app/1.0 (iPhone Simulator; iOS 7.0; Scale/2.00)";

}

下面是我完整的映射和操作代码.像往常一样,任何反馈都会很棒.谢谢.铝

My complete code is for the mapping and operation are below. As usual any feedback would be great. Thanks. Al

- (void)loginMainUser:(NSDictionary*)paramsDict path:(NSString *)apiPath{

RKObjectManager *manager = [RKObjectManager sharedManager];

// Response Mapping
RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[self class]];
[mapping addAttributeMappingsFromDictionary:@{@"token" : @"token",
                                              @"_links" : @"links"}];

[manager addResponseDescriptorsFromArray:@[[RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                        method:RKRequestMethodAny
                                                                                   pathPattern:nil
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]]];
// Request Mapping
RKObjectMapping *userRequestMapping = [RKObjectMapping requestMapping];
[userRequestMapping addAttributeMappingsFromDictionary:@{@"name" : @"first_name",
                                                         @"surname" : @"last_name",
                                                         @"email" : @"email",
                                                         @"password" : @"password"}];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userRequestMapping
                                                                               objectClass:[self class]
                                                                               rootKeyPath:nil
                                                                                    method:RKRequestMethodAny];
[manager addRequestDescriptor:requestDescriptor];


UIImage *image = [UIImage imageNamed:@"logo.png"];

// Serialize the Article attributes then attach a file
NSMutableURLRequest *request = [manager multipartFormRequestWithObject:self
                                                                method:RKRequestMethodPOST
                                                                  path:apiPath
                                                            parameters:nil
                                             constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
                                                 [formData appendPartWithFileData:UIImagePNGRepresentation(image)
                                                                             name:@"logo"
                                                                         fileName:@"logo.png"
                                                                        mimeType:@"image/png"];
                                                                     }];

RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request
                                                                         success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                                                             NSLog(@"Success");
                                                                         } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                                                             NSLog(@"Failed");
                                                                         }];

[manager enqueueObjectRequestOperation:operation];
}

推荐答案

multipartFormRequestWithObject显然不是JSON.这是设计使然. HTTP内容类型已更改,因此JSON和多部分形式互斥.

multipartFormRequestWithObject is explicitly not JSON. This is by design. It's the HTTP content type that is changed so JSON and multipart form are mutually exclusive.

因此,您需要对要实现的目标改变看法.

So, you need to change your mind about what you're trying to achieve.

一种选择是使用映射操作为您的对象创建JSON,然后在调用multipartFormRequestWithObject时提供该JSON.这将为您提供多部分的表单消息,其中包含一段JSON,可以在服务器上反序列化.

One option could be to use a mapping operation to create the JSON for your object and then supply that JSON when you call multipartFormRequestWithObject. This would give you a multipart form message being sent with a section of JSON that could be deserialised on the server.

这篇关于Restkit:multipartFormRequestWithObject不是json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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