如何使用RestKit发布无发布数据
问题描述:
我正在寻找一种方法来映射要发布到端点的空对象。该调用需要为POST
,但不应该有任何数据发布到端点(空体),这只是关于直接调用端点而没有数据。如何使用RestKit发布无发布数据
我已经尝试使用与RestKit: How to handle empty response.body?相同的技巧,但使用RKRequestDescriptor
代替。 “:
未捕获的异常:
RKRequestDescriptor
对象必须与它的目标类是NSMutableDictionary
,得到了一个映射被初始化小号postObject
法‘NSNull’(见[RKObjectMapping requestMapping]
否则使用
postData:nil
在RKObjectMapping
时会导致以下错误);
使用NSNull
为RKRequestDescriptor
的映射似乎工作,但nil
似乎要失败的映射作用。
答
由于错误提到,它正在寻找映射操作的NSMutableDictionary
。所以使用空的NSMutableDictionary
像@{}
而不是nil
为postObject
伎俩。
AFRKHTTPClient *client = [self getClient];
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[NSNull class]];
[objectManager addRequestDescriptor:
[RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:[NSNull class]
rootKeyPath:nil
method:RKRequestMethodAny]];
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[NSNull class]];
[objectManager addResponseDescriptor:
[RKResponseDescriptor responseDescriptorWithMapping:responseMapping
method:RKRequestMethodPOST
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
[objectManager postObject:@{} // <-- this works, but nil doesn't
path:@"/api/some/endpoint"
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
// succes code here
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
// failure code here
}];