iosrestkitrestkit-0.20

RESTKit: Deleting an Object with Body Parameters


RESTKit 0.20.x

I need to send the following DELETE request:

URL: http://rest.domain.com/invite

body: { @"inviteId" : "1234" }

In trying to build that request, below is the code that I'm using:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.objectManager = [self getObjectManager];
self.objectManager.managedObjectStore = appDelegate.managedObjectStore;

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[InviteDelete class]];


RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[mapping inverseMapping] objectClass:[NSMutableDictionary class] rootKeyPath:nil method:RKRequestMethodDELETE];

NSIndexSet *statusCodeSet = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful);

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping
                                                                                        method:RKRequestMethodAny
                                                                                   pathPattern:@"/invites"
                                                                                       keyPath:nil
                                                                                   statusCodes:statusCodeSet];

self.objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
[self.objectManager addRequestDescriptor:requestDescriptor];
[self.objectManager addResponseDescriptor:responseDescriptor];

[self.objectManager.HTTPClient registerHTTPOperationClass:[AFHTTPRequestOperation class]];


InviteDelete *objectToDelete = [[InviteDelete alloc]init];
objectToDelete.inviteId = [NSNumber numberWithInt:294];

[self.objectManager deleteObject:objectToDelete path:@"/invites" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
}];

And Charles Log shows the following request being sent (RAW):

DELETE /invites HTTP/1.1
Host: rest.domain.com
Accept: application/json
Connection: keep-alive
Cookie: connect.sid=PLv05FHG8Al7A84x84mMd.mjlxE3ff3Map
User-Agent: App/1.0 (iPhone Simulator; iOS 7.1; Scale/2.00)
Accept-Language: en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5
Content-Length: 0
Accept-Encoding: gzip, deflate

I know it's easier to create a DELETE when embedded in the URL, where I can just simply add parameters to the objectManager. I wish I had that choice! I have to create a request with body parameters to DELETE.

Requirement: How can I create a simple JSON DELETE request that has the following in the body?

{ @"inviteId" : "1234" }

Optional: It would be nice the RESTKit can also delete the local object upon success.


Solution

  • Your request descriptor is wrong because you use objectClass:[NSMutableDictionary class] so it only applies when you try to delete an NSMutableDictionary instance. You should be using:

    ... objectClass:[InviteDelete class] ...
    

    RestKit has no built in way to understand the response and automatically delete the source object so you need to verify the response content and then perform and save the deletion in the success callback block.