我有两个API:一个是PUT请求,另一个是POST请求。 这两个API都返回完全相同的JSON结构。在我的POST请求上一切都很好,我得到了正确的响应映射。但是在PUT请求中,使用完全相同的映射,响应不会映射。 (Restkit无法找到匹配响应)。 这是POST的代码(这很好):
// POST with no params
GameRequestModel* requestModel = [[GameRequestModel alloc] init];
requestModel.gameid = GAME_ID;
// response
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:[SIAGameModel objectMapping]
method:RKRequestMethodPOST
pathPattern:@"api/v1/games/mygame"
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
// request
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[GAME_ID_KEY]];
// For any object of the model, serialize into an NSMutableDictionary using the given mapping
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[requestMapping inverseMapping]
objectClass:[GameRequestModel class]
rootKeyPath:nil
method:RKRequestMethodPOST];
// set response and request
[self.objectManager addResponseDescriptor:responseDescriptor];
[self.objectManager addRequestDescriptor:requestDescriptor];
[self.objectManager postObject:requestModel
path:[NSString stringWithFormat:@"/api/v1/games/mygame?access_token=%@", accessToken]
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"mygame POST request succeeded");
[self getGameVideos:mappingResult.firstObject];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"mygame POST request failed: %@", error);
}];
这是PUT请求,它没有映射到同一个映射,来自服务器的响应相同:
// PUT with winning video id
RoundRequestModel* roundRequestModel = [[RoundRequestModel alloc] init];
roundRequestModel.selected = @"1";
// response
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:[SIAGameModel objectMapping]
method:RKRequestMethodPUT
pathPattern:@"api/v1/games/mygame"
keyPath:nil
statusCodes:[NSIndexSet indexSetWithIndex:200]];
// request
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromArray:@[@"selected"]];
// For any object of the model, serialize into an NSMutableDictionary using the given mapping
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[requestMapping inverseMapping]
objectClass:[RoundRequestModel class]
rootKeyPath:nil
method:RKRequestMethodPUT];
// set response and request
[self.objectManager addResponseDescriptor:responseDescriptor];
[self.objectManager addRequestDescriptor:requestDescriptor];
NSString* putUrl = @"api/v1/games/mygame";
putUrl = [putUrl stringByAppendingFormat:@"/%@?access_token=%@", appDelegate.gameId, accessToken];
[self.objectManager putObject:roundRequestModel
path:putUrl
parameters:nil
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"mygame PUT request succeeded");
[self getGameVideos:mappingResult.firstObject];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"mygame PUT request failed: %@", error);
}];
感谢。
答案 0 :(得分:0)