1

I am trying to POST an array of json objects (sometimes the array only contains one object) to a REST service using RestKit 0.2

What I want to be sent to the service should look something like this for an array with one object:

{[
      {
         "Latitude":0,
         "Location":"Nowhere, FL",
         "Id":25699,
         "Version":0,
         "Name":"TEST Customer Airport 2 created by tstUser",
         "TimeZone":":America\/New York",
         "Longitude":0,
         "Code":"XXXX987",
         "IsUserDefinedAirportCode":true
      }
]}

Here's what RestKit is sending in request.body according to the trace output:

{
   "\/AirportCode\/UpsertCustomerAirportCodes":[
      {
         "Latitude":0,
         "Location":"Nowhere, FL",
         "Id":25699,
         "Version":0,
         "Name":"TEST Customer Airport 2 created by tstUser",
         "TimeZone":":America\/New York",
         "Longitude":0,
         "Code":"XXXX987",
         "IsUserDefinedAirportCode":true
      }
   ]
}

The first element of the json is the resource/action path of the service I'm calling. Why is RestKit putting that in the Json?

Details...

To send this, I have an ApiAirportCode model object:

#import <Foundation/Foundation.h>

@interface ApiAirportCode : NSObject <NSCoding>

@property (nonatomic, copy) NSString *Code;
@property (nonatomic, copy) NSNumber *Id;
@property (nonatomic, copy) NSNumber *Latitude;
@property (nonatomic, copy) NSString *Location;
@property (nonatomic, copy) NSNumber *Longitude;
@property (nonatomic, copy) NSString *Name;
@property (nonatomic, copy) NSString *TimeZone;
@property (nonatomic, assign) bool IsUserDefinedAirportCode;

@end

Here is the mapping:

RKObjectMapping* airportCodePostMap = [RKObjectMapping mappingForClass:[ApiAirportCode class]];
[airportCodePostMap addAttributeMappingsFromArray:@[@"Code", @"Id", @"Latitude", @"Location", @"Longitude", @"Name", @"TimeZone", @"IsUserDefinedAirportCode"]];
RKRequestDescriptor* airportCodePostReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[airportCodePostMap inverseMapping] objectClass:[ApiAirportCode class] rootKeyPath:@"/AirportCode/UpsertCustomerAirportCodes" method:RKRequestMethodPOST];
[_ObjectManager addRequestDescriptor:airportCodePostReqDescriptor];

Here is the call to the service:

- (void) saveCustomAirportCodesToServer:(NSArray*) customAirports
{
    _ObjectManager.requestSerializationMIMEType=RKMIMETypeJSON;
    [_ObjectManager postObject:customAirports path:@"/AirportCode/UpsertCustomerAirportCodes" parameters:NULL success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {

        // Do something


    } failure:^(RKObjectRequestOperation *operation, NSError *error) {

        ApiPayload* errorPld = [[error userInfo] objectForKey:RKObjectMapperErrorObjectsKey][0];

    }];

    // Do something...
}

Here is the tracing output from RestKit:

2014-05-01 10:17:39.286 Flightdocs[2447:60b] D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation... 2014-05-01 10:17:39.287 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:953 Performing mapping operation: for '__NSDictionaryM' object. Mapping values from object ((null)) to object { } with object mapping (null) 2014-05-01 10:17:39.287 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'Code' to 'Code' 2014-05-01 10:17:39.287 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'Code' to 'Code'. Value: XXXX98765 2014-05-01 10:17:39.287 Flightdocs[2447:12ef7] D restkit.object_mapping:RKPropertyInspector.m:130 Cached property inspection for Class 'NSMutableDictionary': { fileHFSFlags = { isPrimitive = 1; keyValueCodingClass = NSNumber; name = fileHFSFlags; }; fileHFSResourceForkSize = { isPrimitive = 1; keyValueCodingClass = NSNumber; name = fileHFSResourceForkSize; }; } 2014-05-01 10:17:39.287 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'Id' to 'Id' 2014-05-01 10:17:39.288 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'Id' to 'Id'. Value: 25699 2014-05-01 10:17:39.288 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'Latitude' to 'Latitude' 2014-05-01 10:17:39.288 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'Latitude' to 'Latitude'. Value: 0 2014-05-01 10:17:39.288 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'Location' to 'Location' 2014-05-01 10:17:39.288 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'Location' to 'Location'. Value: Nowhere, FL 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'Longitude' to 'Longitude' 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'Longitude' to 'Longitude'. Value: 0 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'Name' to 'Name' 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'Name' to 'Name'. Value: TEST Customer Airport 2 created by tstUser 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'TimeZone' to 'TimeZone' 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'TimeZone' to 'TimeZone'. Value: :America/New York 2014-05-01 10:17:39.289 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'IsUserDefinedAirportCode' to 'IsUserDefinedAirportCode' 2014-05-01 10:17:39.290 Flightdocs[2447:60b] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'IsUserDefinedAirportCode' to 'IsUserDefinedAirportCode'. Value: 1 2014-05-01 10:17:39.290 Flightdocs[2447:60b] D restkit.network:RKObjectParameterization.m:128 Serialized __NSCFBoolean value at keyPath to __NSCFBoolean (1) 2014-05-01 10:17:39.290 Flightdocs[2447:60b] D restkit.object_mapping:RKMappingOperation.m:1021 Finished mapping operation successfully... 2014-05-01 10:17:39.294 Flightdocs[2447:60b] T restkit.network:RKObjectRequestOperation.m:178 POST 'https://rsw-dev-05.flightdocs.com/AirportCode/UpsertCustomerAirportCodes': request.headers={ Accept = "application/json"; "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"; Authorization = "Bearer 1QgmbgUMk5Z-g5W8BtDZjOIok_NIAVuaL9b6kJJybdObQv3MZVShxWS098L1M1iP7DcGsV2nYfIK-FE6409hPN8WVQAJdNwh1VKRCY0STTKpViKown53Mq0B5cl-EbgIhBOF0LvTf0xwQnsLBAOl-21QYmZyNzfEbXGNhNuX3Cn06oHdZIzxLvpvwTATnVG-hCTQvAl04Oh3wzKWu3UAt7Nnbe_pZDIhLCpPFwyIw3aI2ife5fbWpoEcRBGbU0Bob8o2vrCciQiLWPCqsN2sepztD6A1XIPWObPhgn2sBGyrgSaJN_MyG6141UAKGBTDyYPWVMIVA6wx-AFtc6116goKtHc-FV7COJl_TuAWw7itc-543Rgmu_m1jKmRQT78bzit1JxG_j0YsJ4nB2hVHdUstV3QKAGiPUQl9PJxBRyHiUhwkRFwxIEAtSk9NsjfeehJJjaEq9Dr3jkfh6S8M6-AUa88q8DxNE-xman84dUsqeBBVigoVP-PGkgqv8fkU3DE2M4vVYdlyAHGvR8okA"; "Content-Type" = "application/json; charset=utf-8"; "User-Agent" = "Flightdocs/1.4.7 (iPad Simulator; iOS 7.1; Scale/1.00)"; } request.body={"/AirportCode/UpsertCustomerAirportCodes":[{"Id":25699,"Latitude":0,"Location":"Nowhere, FL","Name":"TEST Customer Airport 2 created by tstUser","TimeZone":":America/New York","Longitude":0,"Code":"XXXX98765","IsUserDefinedAirportCode":true}]}

1 Answer 1

1

I figured it out...

My issue was with the rootKeyPath parameter to RKRequestDescriptor requestDescriptorWithMapping. This should be nil.

I was passing in the resource/action name because I thought RestKit was using this to know when to use this request descriptor (just like for mapping responses with the withPathPattern parameter). Not sure why there ins't the need to pass something to indicate the withPathPattern.

So this line of code:

RKRequestDescriptor* airportCodePostReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[airportCodePostMap inverseMapping] objectClass:[ApiAirportCode class] rootKeyPath:@"/AirportCode/UpsertCustomerAirportCodes" method:RKRequestMethodPOST];

should be

RKRequestDescriptor* airportCodePostReqDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[airportCodePostMap inverseMapping] objectClass:[ApiAirportCode class] rootKeyPath:nil method:RKRequestMethodPOST];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.