Search code examples
iosjsonrestkitrestkit-0.20rkobjectmapping

Retrived null in POST JSON RestKit


I have used RestKit and made a mapping with a managed object. I then use the postObject method but I have a problem when retrieved the body as it maps to null.

 RKObjectManager  *objectManager = [RKObjectManager managerWithBaseURL:baseURL];

// Serialize to JSON
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[RKObjectManager setSharedManager:objectManager];

RKEntityMapping *searchInfoMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Search class]) inManagedObjectStore: objectManager.managedObjectStore];
searchInfoMapping.identificationAttributes = @[ @"faresQueryType" ];

[searchInfoMapping addAttributeMappingsFromDictionary:@{
    @"id" : @"ID",
    @"type" : @"Type",
    @"count" : @"Count",
    @"route” : @"route”,
}];

RKObjectMapping     *searchInfoRequestMapping =[RKObjectMapping requestMapping];
[searchInfoRequestMapping addAttributeMappingsFromDictionary:@{
    @"id" : @"ID",
    @"type" : @"Type",
    @"count" : @"Count",
    @"route” : @"route”,
}];

//Data mapping is a method that returns an RKObjectMapping for my model
RKResponseDescriptor * searchDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:searchInfoMapping pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptorsFromArray:@[searchDescriptor,]];

//Inverse mapping, to perform a POST

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:searchInfoRequestMapping  objectClass:[Search class] rootKeyPath:nil];
[objectManager addRequestDescriptor:requestDescriptor];


[appDelegate.objectManager postObject:nil path:@"routes" parameters:postParameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
            NSLog(@"Success case %@",mappingResult);
        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
            NSLog(@"Failure in Flight Mapping %@",error);
            [alert show];
        }];

Edit

      //JSON Result 

    {
    "currency": {
        "code": "USD",
        "name": "U.S. Dollar",
        "symbol": "US$",
        "exchange_rate": 1
    },
    "routes": [
        {
            "id": "MS648-2022:MS985-2110",
            "fares": [
                {
                    "price": 745.32,
                    "description": "Economy",
                    "deeplink": "http://www.wego.com/flights/providers/2/deeplinks?search_id=CQtxCXQCRfmwhp72PAjopQ&trip_id=RUH:NYC:2013-12-20&fare_id=sky-tours.com:0&route=RUH-JFK",
                    "provider_code": "sky-tours.com",
                    "deeplink_params": {
                        "trip_id": "RUH:NYC:2013-12-20",
                        "route": "RUH-JFK",
                        "search_id": "CQtxCXQCRfmwhp72PAjopQ",
                        "fare_id": "sky-tours.com:0"
                    }
                }
            ]
        }
    ]
}

Solution

  • You need to supply the Search object instead of nil when you call postObject (currently you have postObject:nil). This tells RestKit which request mapping to use to send the request. As a result of not setting it the server will likely not have all of the required information (it's impossible to tell with the information provided).