Search code examples
iosobjective-crestkit

Restkit request.body values between square brackets


I'm doing a very basic POST request. setting my requestSerializationMIMEType into RKMIMETypeFormURLEncoded as my server would expect (although it's the default).

Now in the log the request.body would look like this:

request.body=[param1]=test&[param2]=2724

Resulting unknown form values in the server.

The problem here is the param names, they're between square brackets, which I don't have an explanation why they're serialised like this!

My code is somehow exactly the same of the example provided on github.

NOTE: I have done the POST request manually without the square brackets and it's working fine.

EDIT

The code

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GenericResponse class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"error":@"error", @"status":@"status"}];
NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); 
RKResponseDescriptor *genericDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:@"" keyPath:@"" statusCodes:statusCodes];

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; 
[requestMapping addAttributeMappingsFromDictionary:[User generateJSONMapping]];//dictionary mapping
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"" method:RKRequestMethodPOST];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://foo.com/rest/createUser/"]];
[manager addRequestDescriptor:requestDescriptor];
[manager addResponseDescriptor:genericDescriptor];
manager.requestSerializationMIMEType=RKMIMETypeFormURLEncoded;

// POST to create
[manager postObject:user path:@"" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSLog(@"success");// the request is success but the params are not delivered to the server 
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"fail");
}];

The User is very simple I guess

@interface User : NSObject
   @property NSNumber *idShort;
   @property NSNumber *idLong;
   @property NSNumber *idCom;
   @property NSString *comment;
   @property NSString *token;

   + (NSDictionary *) generateJSONMapping;
@end


@implementation User
   + (NSDictionary *) generateJSONMapping
   {
      return @{
         @"idShort": @"idShort",
         @"idLong": @"idLong",
         @"idCom":@"idCom",
         @"comment":@"comment",
         @"token":@"token",
         };
   }
@end

Solution

  • The problem is in the RKRequestDescriptor, passing rootKeyPath as an empty string would cause this. Passing nil would solve the problem.

    RKRequestDescriptor *requestDescriptor = [
    RKRequestDescriptor requestDescriptorWithMapping:requestMapping
    objectClass:[User class]
    rootKeyPath:nil
    method:RKRequestMethodPOST];