我正在尝试向服务器发送POST请求。我正在使用RESTKit和请求描述符:
RKObjectMapping *messageRequestMapping = [RKObjectMapping requestMapping ];
[messageRequestMapping addAttributeMappingsFromArray:@[@"to", @"msg"]];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:messageRequestMapping objectClass:[PostMessage class] rootKeyPath:nil method:RKRequestMethodAny];
[objectManager addRequestDescriptor:requestDescriptor];
然后我正在使用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");
}];
问题是,即使我看到POST请求成功进入服务器,也始终会调用失败块。当我使用简单的REST客户端测试相同的POST请求时,我获得了200的状态代码。
我得到的错误是:
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.}
我不确定是否必须设置responseDescriptor。 我不能像设置responseDescriptor时那样设置预期的状态代码吗?
修改: 我添加了一个响应描述符:
// 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]];
作为服务器的响应是:
{
"success" : "Message sent successfully."
}
并且仍然以相同的错误失败。
答案 0 :(得分:1)
错误消息告诉您
"在搜索的关键路径中找不到可映射的对象表示。"
所以,要么你给了一个不正确的响应描述符,要么你没有定义一个。 给出正确的响应描述符应该可以解决问题。
这是一个示例响应描述符:
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:<YOURCLASSHERE>];
...
RKResponseDescriptor * responseDescriptor = [RKResponseDescriptor
responseDescriptorWithMapping:mapping
method:RKRequestMethodPOST
pathPattern:path keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
答案 1 :(得分:0)
我实际上并没有发现我的响应描述符有什么问题。 我处理这个问题的方法是更改服务器上的响应。服务器现在发送的响应代码为201,而RESTKit不再发生故障,而不是发送回json文件。