0
votes

i have two APIs: one is a PUT request and the other is a POST request. both of those APIs returns exactly the same JSON structure. on my POST request all is fine, and i get the response mapped correctly. but on the PUT request, with the using the exact same mapping, the response doesn't map. (Restkit can't find a match response). this is the code for the POST (this is working fine):

    // 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);

                           }];

and this is the PUT request, which does not map to the same mapping, with the same response from the server:

    // 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);

                          }];

thanks.

1

1 Answers

0
votes

ok, got the problem. when calling the PUT request, the url is different since I'm adding the game id to the url (instead of .../mygame it's now .../mygame/gameid) and this should be the pathPattern for this call. so there was a mismatch between the request and response pathPattern, hence the error....