RESTKit 0.20.x
我需要发送以下DELETE请求:
网址:http://rest.domain.com/invite
正文:{@“inviteId”:“1234”}
在尝试建立该请求时,以下是我正在使用的代码:
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) {
}];
Charles Log显示以下请求正在发送(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
我知道将URL嵌入时创建
DELETE
会更容易,我可以在其中简单地将parameters
添加到objectManager
中。我希望我有这个选择!我必须创建一个带有DELETE
的正文参数的请求。要求:如何创建一个简单的JSON
DELETE
请求,该请求中包含以下内容?{@“inviteId”:“1234”}
可选:很好,RESTKit也可以在成功后删除本地对象。
最佳答案
您的请求描述符是错误的,因为您使用了objectClass:[NSMutableDictionary class]
,因此仅在您尝试删除NSMutableDictionary
实例时才适用。您应该使用:
... objectClass:[InviteDelete class] ...
RestKit没有内置的方式来理解响应并自动删除源对象,因此您需要验证响应内容,然后执行删除并将其保存在
success
回调块中。
关于ios - RESTKit:删除带有主体参数的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23774626/