0

I got a problem with a request mapping in Restkit 0.20. I want to put a NSArray with NSStrings into the request under the the key "mails" e.g.:

{mails:[@"first@gmail.com",@"second@gmail.com"]}

So, for this case I don't really need an object mapping, cause I am using only standard objects. I just didn't get it to work, so I switched back to the regular way (at least for me) - introducing a DTO object MailRequest which contains the NSArray. I do it like this:

RKObjectMapping* mapping =  [RKObjectMapping requestMapping];
[mapping addAttributeMappingsFromDictionary:@{
        @"mails":@"mails"
}];  

RKRequestDescriptor *reqDesc = 
            [RKRequestDescriptor requestDescriptorWithMapping:mapping
                                                  objectClass:[MailRequest class]
                                                  rootKeyPath:nil];

RKObjectManager *manager = ...
...
NSMutableURLRequest *request = [manager requestWithObject:requestObject 
                                                   method:RKRequestMethodPOST 
                                                     path:urlString parameters:nil];

RKObjectRequestOperation *operation = 
                     [manager objectRequestOperationWithRequest:request ...

... but I would like to get rid of the MailsRequest DTO object. Is that possible?

abbood
  • 23,101
  • 16
  • 132
  • 246
cloudnaut
  • 982
  • 3
  • 13
  • 35
  • If you have an object that has an array relationship to some other object which has the string as a property it should be. Show the data structure where the strings are stored. – Wain May 24 '13 at 15:14
  • Hi, I don't want to use an object (like MailsRequest) as I did in my example. Instead I wanna put the array directly into the request. – cloudnaut May 24 '13 at 15:36
  • So why not just create the JSON and NSURLRequest yourself (with the JSON data), then pass to RKObjectRequestOperation ? – Wain May 24 '13 at 15:40
  • Sounds good, do you have a small example? – cloudnaut May 24 '13 at 15:49

1 Answers1

2

Skip the mapping step and just use RestKit for the send and any response mapping. Build your dictionary any way you want. You do need to create the URL from urlString.

NSDictionary *mails = @{mails:@[@"first@gmail.com",@"second@gmail.com"]};
NSError *error = nil;
NSData *mailsJSON = [NSJSONSerialization dataWithJSONObject:mails options:0 error:&error];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:mailsJSON];

RKObjectRequestOperation *operation = [manager objectRequestOperationWithRequest:request ...
Wain
  • 118,658
  • 15
  • 128
  • 151