iosjsonrestkitrestkit-0.20restkit-0.24.x

Can't map received object after a POST


After doing a POST request to the server, the returned object is un-mappable, although I've created all the necessary infrastructure for it.

To simplify my problem, I've created a "POST object" class, and another one for the response object; (below are the important bits of my code)

JSON

Request

{ 
    "USERNAME" : 280S808D08s,
    "PASSWORD" : "lkjl123kjl",
    "DEVICE" : 12345
}

Response

{
    "firstname": "first name here",
    "lastname": "last name here",
    "mobile": "012938012839",
    "email": "john@smith.com",
    "expiry": "2016-05",
    "authorized": true
}

This is my "POST object" class implementation

@interface Login : NSObject

@property NSString *USERNAME;
@property NSString *PASSWORD;
@property NSString *DEVICE;

@end

these are the API methods;

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Login class]];
[mapping addAttributeMappingsFromArray:@[@"USERNAME", @"PASSWORD",@"DEVICE"]];

RKRequestDescriptor *loginRequest = [RKRequestDescriptor
                                     requestDescriptorWithMapping:[mapping inverseMapping]
                                     objectClass:[Login class]
                                     rootKeyPath:nil
                                     method:RKRequestMethodPOST];

The actual request

[objectManager addRequestDescriptor:loginRequest];

// POST
Login* loginUser = [Login new];
loginUser.USERNAME = @"96150001505233";
loginUser.PASSWORD = @"gb168362";
loginUser.DEVICE = @"12345";

[[RKObjectManager sharedManager] postObject:loginUser
                                       path:@"/app_dev.php/api/v2/user/login"
                                 parameters:nil
                                    success:^(RKObjectRequestOperation  *operation, RKMappingResult *mappingResult) {
                                    } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                    }];

And now the class of the "RECEIVED Object"

@interface User : NSObject

@property NSString *firstname;
@property NSString *lastname;
@property NSString *email;
@property NSString *mobile;

@end

The API methods:

RKObjectMapping *userMap = [RKObjectMapping mappingForClass:[User class]];
[userMap addAttributeMappingsFromArray:@[ @"firstname", @"lastname", @"email", @"mobile"]];

RKResponseDescriptor *userResponse = [RKResponseDescriptor
                                      responseDescriptorWithMapping:userMap
                                      method:RKRequestMethodPOST
                                      pathPattern:@"user/login"
                                      keyPath:nil
                                      statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

[objectManager addResponseDescriptor: userResponse];

ERROR

I've spaced it neatly to make it more readable.

Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No response descriptors match the response loaded."

UserInfo={NSLocalizedFailureReason=A 200 response was loaded from the URL 'http://mywebsite.com/app_dev.php/api/v2/user/login', which failed to match all (0) response descriptors:,

NSErrorFailingURLStringKey=http://mywebsite.com/app_dev.php/api/v2/user/login,

NSErrorFailingURLKey=http://mywebsite.com/app_dev.php/api/v2/user/login,

NSUnderlyingError=0x7fee81d59440 {Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched."

UserInfo={NSLocalizedDescription=No mappable object representations were found at the key paths searched.,

NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched: The representation inputted to the mapper was found to contain nested object representations at the following key paths: authorized, email, expiry, firstname, lastname, mobile

This likely indicates that you have misconfigured the key paths for your mappings., keyPath=null, DetailedErrors=( )}}, keyPath=null, NSLocalizedDescription=No response descriptors match the response loaded.}

Please help :(


Solution

  • The solution was rather simple. My keyPath was wrong for the POST request. Just had to modify it to this;

    [[RKObjectManager sharedManager] postObject:loginUser
                                           path:@"user/login"
                                     parameters:nil
                                        success:^(RKObjectRequestOperation  *operation, RKMappingResult *mappingResult) {
                                        } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                        }];