通过RestKit形成多部分请求

时间:2015-05-06 08:19:22

标签: objective-c afnetworking restkit multipartform-data

这是我将多部分数据发布到服务器的代码。我观察到的是请求中只包含图像数据,而不是文章对象。如果有人在过去成功完成了这些工作,请告诉我。提前谢谢。

 RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"title", @"author", @"body"]];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping 
                                    objectClass:[Article class] rootKeyPath:@"article" method:RKRequestMethodAny];

[manager addRequestDescriptor:requestDescriptor];

Article *article = [Article new];
article.title = @"Introduction to RestKit";
article.body = @"This is some text.";
article.author = @"Blake";

NSMutableURLRequest *request = [objectManager multipartFormRequestWithObject:article
                                                                      method:RKRequestMethodPOST
                                                                        path:path
                                                                  parameters:nil
                                                   constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {

                                                       [formData appendPartWithFileData:imageData
                                                                                   name:imageInfo
                                                                               fileName:imageName
                                                                               mimeType:@"image/jpeg"];

                                                   }];

RKObjectRequestOperation *operation1 = [objectManager objectRequestOperationWithRequest:request
                                                                               success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{

    // Process data
    NSLog(@"success");

} failure:^(RKObjectRequestOperation *operation, NSError *error) {

    // An error occurred
    NSLog(@"error");

}];

1 个答案:

答案 0 :(得分:0)

使用appendPartWithHeaders解决了这个问题。 Source。 Lemme知道是否有人需要更密切地看待实施。