Search code examples
iosobjective-ccore-datarestkitrestkit-0.20

RestKit .20 RKRequestDescriptor postObject - Expected status code in (400-499), got 200


Having a bit of a strange problem, I'm using RestKit to post a CoreData object to a remote web service. Everything on the remote end looks to be working fine but it seems like RestKit is having some issues mapping the response.

My object response mappings look like this, I have a User class with a UserProfile one to one relationship:

 //UserProfile
 RKEntityMapping * userProfileMapping =
        [RKEntityMapping mappingForEntityForName:NSStringFromClass([UserProfile class])
                            inManagedObjectStore:[manager managedObjectStore]];
//…mapping
//User
RKEntityMapping * userMapping =
        [RKEntityMapping mappingForEntityForName:NSStringFromClass([User class])
                            inManagedObjectStore:[manager managedObjectStore]];

//…mapping

//relationship mapping
[userMapping addPropertyMapping:[RKRelationshipMapping 
     relationshipMappingFromKeyPath:@"userProfile"
                          toKeyPath:@"userProfile"
                        withMapping:userProfileMapping]];

 //add the response mappings for GET success
 [manager addResponseDescriptorsFromArray:@[
        [RKResponseDescriptor responseDescriptorWithMapping:userMapping
                                                     method:RKRequestMethodGET
                                                pathPattern:nil
                                                    keyPath:@"currentUser"
                     statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]] …rest of my mappings];

The request mapping is the inverse of userMapping

  RKRequestDescriptor * currentUserRequestDescriptor =
            [RKRequestDescriptor requestDescriptorWithMapping:[userMapping inverseMapping]
                                                  objectClass:[User class]
                                                  rootKeyPath:@"currentUser"
                                                       method:RKRequestMethodPOST];

  [manager addRequestDescriptor:currentUserRequestDescriptor];

My GET requests on the User class are working fine, POST is also working correctly.

To update my User coreData object I'm using `postObject'

[objectManager postObject:updatedUser
                     path:@"update"
               parameters:nil
                  success:…handler
                  failure:…handler]

The POST works correctly and the object is saved in my remote service. However, my success handler is never called and I never get the updated User object. I have logging on and I see the valid JSON in the response.

response.body={"currentUser":{….user object}}

Instead, failure is called, it seems to want a 400 response instead of the 200 my service is responding with.

"NSLocalizedDescription" -> "Expected status code in (400-499), got 200"

I don't see a way to set the expected statusCodes on RKRequestDescriptor like on RKResponseDescriptor.

Thanks


Solution

  • Because your User response descriptor uses method:RKRequestMethodGET so it won't be considered as a match for the response from your POST request.

    Instead, set the method to RKRequestMethodAny.

    Presumably RestKit is expecting a 400-499 because you have a response descriptor configured to handle error responses at that path.