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.