RESTKit 0.20.x
我需要发送以下DELETE请求:
网址:http://rest.domain.com/invite
body:{@" 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
我知道在嵌入网址时创建DELETE
会更容易,我只需将parameters
添加到objectManager
。我希望我有这个选择!我必须使用DELETE
的body参数创建一个请求。
要求:如何创建一个在正文中包含以下内容的简单JSON DELETE
请求?
{@" inviteId" :" 1234" }
可选:RESTKit也可以在成功时删除本地对象。
答案 0 :(得分:0)
您的请求描述符是错误的,因为您使用objectClass:[NSMutableDictionary class]
因此仅在您尝试删除NSMutableDictionary
实例时才适用。你应该使用:
... objectClass:[InviteDelete class] ...
RestKit没有内置的方法来理解响应并自动删除源对象,因此您需要验证响应内容,然后在success
回调块中执行并保存删除。
答案 1 :(得分:0)
您无法通过对象映射获取删除请求的正文。 Rest API设计支持DELETE body HTTP 1.1标准
也是如此Rest工具包不支持对象映射,但它肯定允许您创建自定义请求和操作
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];