ios - RestKit - postObject 跳过参数

标签 ios objective-c core-data restkit restkit-0.20

我正在尝试POST 一个对象到服务器,但它不起作用。这就是我正在做的。仅供引用,我使用 MagicalRecords 来轻松处理 CoreData

DBComments *comment = [DBComments MR_createEntity];
comment.body = @"This is body";
comment.subject = @"This is subject";
comment.commentable_id = [NSNumber numberWithInt:291110];
comment.send_email = [NSNumber numberWithBool:YES];

[[RKObjectManager sharedManager] postObject:comment path:URL_COMMENTS
                                     parameters:nil
                                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                            KLog(@"success");
                                        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                            KLog(@"fail");
                                        }];

但我得到以下回应:

E restkit.network:RKObjectRequestOperation.m:576 Object request failed: Underlying HTTP request operation failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x11b688c0 {NSLocalizedRecoverySuggestion={"error":"body is missing, commentable_id is missing"}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0xc632e50> { URL: http://10.28.79.98:3002/comments }, NSErrorFailingURLKey=http://10.28.79.98:3002/comments, NSLocalizedDescription=Expected status code in (200-299), got 400, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xc23cf70> { URL: http://10.28.79.98:3002/comments } { status code: 400, headers {
    "Cache-Control" = "no-cache";
    Connection = "Keep-Alive";
    "Content-Length" = 54;
    "Content-Type" = "application/json";
    Date = "Fri, 28 Mar 2014 07:45:01 GMT";
    Server = "WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)";
    "X-Request-Id" = c8b765e61a8d44020be26e9b3267096e;
    "X-Runtime" = "0.010554";
    "X-Ua-Compatible" = "IE=Edge";
} }}

正如您在响应中看到的那样,没有发布值

{"error":"body is missing, commentable_id is missing"}

这是我的映射配置

[objectManager addRequestDescriptorsFromArray:@[
                                                    // Comments
                                                    [RKRequestDescriptor requestDescriptorWithMapping:[self commentsRequestMapping]
                                                                                          objectClass:[DBComments class]
                                                                                          rootKeyPath:@""
                                                                                               method:RKRequestMethodPOST],                                                                                                        
                                                    ]];

- (RKObjectMapping *)commentsRequestMapping {
    RKObjectMapping *commentsRequestMapping = [RKObjectMapping requestMapping];
    [commentsRequestMapping addAttributeMappingsFromDictionary:@{
                                                                 @"body" : @"body",
                                                                 @"commentable_id" : @"commentable_id",
                                                                 @"commentable_type" : @"commentable_type",
                                                                 @"created_at" : @"created_at",
                                                                 @"id" : @"id",
                                                                 @"lft" : @"lft",
                                                                 @"parent_id" : @"parent_id",
                                                                 @"rgt" : @"rgt",
                                                                 @"subject" : @"subject",
                                                                 @"title" : @"title",
                                                                 @"updated_at" : @"updated_at",
                                                                 @"user_id" : @"user_id",
                                                                 @"send_email" : @"send_email",
                                                                 }];

    return commentsRequestMapping;
}

我从来没有能够成功地使用postObject。我错过了什么吗?请帮忙。

最佳答案

如果服务器期望一组简单的内容,那么请求描述符应该有一个 nil 键路径,而不是一个空字符串。

重要的是您发送的内容与服务器期望的内容相匹配,您应该结合使用跟踪日志记录和 Charles(或类似工具)来验证您的代码实际生成的内容。

关于ios - RestKit - postObject 跳过参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22707142/

相关文章:

objective-c - 在 iOS 4 中使用 Block Completion Handler 制作动画

iphone - 新版本App内购

ios - 元 "apple-itunes-app"不起作用

ios - [String : AnyObject] be an AnyObject, 但 [String : AnyObject? ] 怎么可能不行?

ios - Gigya Swift Facebook 和 Google Native 登录

c - 使用 C 或 Objective C 求逆指数

iPhone:从根据索引排序的核心数据中获取数据

cocoa - NSManagedObject 未保存——谨防反向关系

ios - 使用 Core Data 和 validateForUpdate - 如何将焦点设置到文本框。

ios - 无法在 tableViewcell 中设置 UILabel 填充