Question

I am trying to POST an object to server, but it is not working. This is what I am doing. FYI, I am using MagicalRecords for CoreData easy handling.

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

But I get following response:

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

As you can see in response, the values are not being POSTed

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

Here is my mapping config

[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;
}

I have never been able to use postObject successfully. Am I missing something? Please help.

Was it helpful?

Solution

If the server is expecting a flat set of content then the request descriptor should have a nil key path, not an empty string.

It's important that what you send matches what the server expects and you should use a combination of trace logging and Charles (or a similar tool) to verify what your code is actually producing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top