如何使用RestKit(0.20)执行DELETE请求?

时间:2013-07-29 03:52:51

标签: restkit restkit-0.20

我正在尝试使用RestKit发送DELETE请求,但似乎它总是以“GET”发送。这是我的代码:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
                                       responseDescriptorWithMapping:[self objectMapping]
                                       method:RKRequestMethodDELETE
                                       pathPattern:nil
                                       keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

    [delegate onRequestSuccess:mappingResult.array];

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"operation failed with error: %@", error);
    [delegate onRequestError:operation message:error];
}];

[objectRequestOperation start];

我还尝试使用RKObjectManager:deleteObject,它正确发送DELETE请求,但响应没有被映射。

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:request.url];
[manager addResponseDescriptor:responseDescriptor];
[manager deleteObject:nil path:request.urlString parameters:nil
              success:^(RKObjectRequestOperation *operation , RKMappingResult *mappingResult) {
                  Tag *tag = mappingResult.firstObject; // this is null, does not get mapped

              } failure:^(RKObjectRequestOperation *operation , NSError *error) {
                  RKLogError(@"Error deleting tag %@, error: %@", tagId, error);
              }];

1 个答案:

答案 0 :(得分:1)

如果您使用RKObjectRequestOperation,则需要自己配置request。它是GET,因为这是默认值。

如果您使用RKObjectManager,那么您可以使用deleteObject来代替您。{/ p>