0

I was looking for a way to map an empty object to be posted to an endpoint. The call needs to be a POST, but there shouldn't be any data posted to the endpoint (empty body), it's only about calling the endpoint directly without data.

I've tried doing the same trick as in RestKit: How to handle empty response.body? but using RKRequestDescriptor instead.

Doing so leads to following error when using postData:nil in RKObjectMapping's postObject method:

Uncaught exception: RKRequestDescriptor objects must be initialized with a mapping whose target class is NSMutableDictionary, got 'NSNull' (see [RKObjectMapping requestMapping]);

Using NSNull for the RKRequestDescriptor's mapping seems to work, but nil seems to fail the mapping action.

Community
  • 1
  • 1
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81

1 Answers1

1

As the error mentions, it's looking for an NSMutableDictionary for the mapping action. so using an empty NSMutableDictionary like @{} instead of nil for postObject does the trick.

AFRKHTTPClient *client = [self getClient];

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

RKObjectMapping *requestMapping = [RKObjectMapping mappingForClass:[NSNull class]];
[objectManager addRequestDescriptor:
 [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                       objectClass:[NSNull class]
                                       rootKeyPath:nil
                                            method:RKRequestMethodAny]];

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[NSNull class]];
[objectManager addResponseDescriptor:
 [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                              method:RKRequestMethodPOST
                                         pathPattern:nil
                                             keyPath:nil
                                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

 [objectManager postObject:@{} // <-- this works, but nil doesn't
                         path:@"/api/some/endpoint"
                   parameters:nil
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                          // succes code here
                      } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                          // failure code here
                      }];
GabLeRoux
  • 16,715
  • 16
  • 63
  • 81