2

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
user1107173
  • 10,334
  • 16
  • 72
  • 117

2 Answers2

0

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.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • There is also this http://stackoverflow.com/questions/19218093/delete-object-in-restkit-20-did-not-take-json-values/19222399?noredirect=1#comment36585183_19222399 – Wain May 21 '14 at 16:59
  • Thanks. I changed it to objectClass to [InviteDelete class] and the raw request appears to be the same. – user1107173 May 21 '14 at 17:01
  • See the other answer I linked to, I don't think you can do it without editing RestKit code (forgot that at the time I wrote this answer). It could be good to patch RestKit to check for an exact match on the `method` and perform the mapping if it's an explicit request (then raise a pull request)... – Wain May 21 '14 at 18:17
  • How do you make a delete request with a body ? Should I use NSURLConnection? – user1107173 May 21 '14 at 18:22
  • You could. Or create a request and then use `objectRequestOperationWithRequest:success:failure:`. And you could create the JSON body with `RKMappingOperation`. Or you could request the endpoint to change to `/invite/:id` – Wain May 21 '14 at 18:32
0

You can't have a body for delete requests by object mapping. Rest API design supports DELETE body So does HTTP 1.1 standards

Rest kit does't support object mapping but it surely allows you to create custom request and operations

NSDictionary *params = YOUR_JSON_BODY

NSMutableURLRequest *request = [self requestWithPathForRouteNamed:path
                                                           object:objectToDelete
                                                       parameters:nil];
[request setHTTPBody:[NSJSONSerialization dataWithJSONObject:params options:NSJSONReadingMutableLeaves error:nil]];
[request setHTTPMethod:@"DELETE"];
RKObjectRequestOperation *operation = [self objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
   if (success) ...
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    if (failure) failure(error);
}];

[self enqueueObjectRequestOperation:operation];
Community
  • 1
  • 1
Kunal Balani
  • 4,739
  • 4
  • 36
  • 73