0
votes

I'm trying to send a POST request to a server. I'm using RESTKit with a request descriptor:

RKObjectMapping *messageRequestMapping = [RKObjectMapping requestMapping ];
[messageRequestMapping addAttributeMappingsFromArray:@[@"to", @"msg"]];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:messageRequestMapping objectClass:[PostMessage class] rootKeyPath:nil method:RKRequestMethodAny];

[objectManager addRequestDescriptor:requestDescriptor];

and then I'm using postObject:path:parameters:success:failure:

PostMessage *postMessage = [PostMessage new];
    postMessage.to = self.pickedPerson.name;
    postMessage.msg = message.messgaeContent;
    [[RKObjectManager sharedManager] postObject:postMessage path:@"/post" parameters:nil
                                        success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                            NSLog(@"SUCCES: message sent to server");

                                        }
                                        failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                            NSLog(@"WARNING");
                                              }];

The problem is that the failure block always gets called even though I see that the POST request got to the server successfully. When I test the same POST request with a simple REST client I get a status code of 200.

The error I get is:

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded." UserInfo=0x10c67adf0 {NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://bla.com/post', which failed to match all (0) response descriptors:, NSErrorFailingURLStringKey=http://bla.com/post, NSErrorFailingURLKey=http://bla.com/post, NSUnderlyingError=0x10c60add0 "No mappable object representations were found at the key paths searched.", keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}

I'm not sure if I have to set a responseDescriptor or not. Can't I just set the expected status code like I do when setting a responseDescriptor?

EDIT: I've added a response descriptor:

// message response after a post
    RKObjectMapping *messageResponseForPostMapping = [RKObjectMapping mappingForClass:[PostMessage class]];
    [messageResponseForPostMapping addAttributeMappingsFromArray:@[@"success"]];

RKResponseDescriptor *messageResponseForPostDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:messageResponseForPostMapping
                                             method:RKRequestMethodPOST
                                        pathPattern:@"post"
                                            keyPath:@""
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]]; 
[objectManager addResponseDescriptorsFromArray:@[messageResponseDescriptor, personResponseDescriptor, messageResponseForPostDescriptor]];

as the server's response is:

{
  "success" : "Message sent successfully."
}

and it still fails with the same error.

2
I don't think the response structure matches the KVC pattern of the descriptor. - Kyle Emmanuel
what do you mean? I'm new to RESTKit so I don't know it very well - bobsacameno
The response structure should match the structure of the descriptor. - Kyle Emmanuel
I added a response descriptor that matches the response, and it still fails with the same error (I edited the question above) - bobsacameno

2 Answers

1
votes

The error message tells you that

"No mappable object representations were found at the key paths searched."

So, either you gave an incorrect response descriptor, or you did not define one. Giving a correct response descriptor should fix the issue.

This is an example response descriptor:

RKObjectMapping* mapping = [RKObjectMapping mappingForClass:<YOURCLASSHERE>];
...
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor
    responseDescriptorWithMapping:mapping
    method:RKRequestMethodPOST
    pathPattern:path keyPath:nil
    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
0
votes

I didn't actually find what's wrong with my response descriptor. My way of dealing with this was to change the response on the server. Instead of sending back a json file, the server now sends response code of 201, and RESTKit does not fail anymore.