RestKit object mapping request that has nested JSON in its HTTP body

1.3k views Asked by At

I am making a POST request with a nested JSON in its HTTP body and I am not getting the appropriate response. I am passing a custom object called RequestObject with properties:

@interface RequestObject : NSObject
@property (nonatomic, copy) NSString *title;
@property (nonatomic, strong) NSDictionary *location;
@property (nonatomic, strong) NSMutableArray *pictures;

I want to map my object such that in its HTTP Body, the JSON would look like this:

{
        "title": "Magic School Bus",
        "location": {
            "latitude": "38.764792",
            "longitude": "-121.247574"
        },
        "pictures": [
            {
                "base64pic": "iVBORw0KGg..."
            }
        ]
}

For the request object, I configured it like so:

RequestObject *request = [[RequestObject alloc] init];

// Title property
request.title = @"MyTitle";

// Location property
request.location = @{
              @"latitude" : [NSNumber numberWithDouble:38.757965],
              @"longitude" : [NSNumber numberWithDouble:-121.254044]
              };

// Pictures property
request.pictures = [[NSMutableArray alloc] init]; 

// I add a dictionary into the pictures array
NSDictionary *picture = @{@"base64pic" : @"somebase64string"};
[request.pictures addObject:picture];

I believe I may be setting my request.pictures incorrectly and that is why my response comes back wrong. This is the rest of my code for calling the POST request:

RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[mapping addAttributeMappingsFromArray:@[@"title", @"location", @"pictures"]];
[manager addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[RequestObject class] rootKeyPath:nil method:RKRequestMethodPOST]];

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[NSDictionary class]];
[mapping addAttributeMappingsFromArray:@[@"response"]];
[manager addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodPOST pathPattern:nil keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

// Post
[manager postObject:request path:@"books/" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *result){

    // Response back
}failure:^(RKObjectRequestOperation *operation, NSError *error) {

When I make the POST call in my project, I see this as the HTTP Request body in my console:

Request Body : location[latitude]=38.757965&location[longitude]=-121.254044&pictures[][base64pic]=somebase64string&title=MyTitle
2

There are 2 answers

0
dispatchMain On

You can just create an NSMutableURLRequest and setHTTPBody with your request data and create the RKObjectRequestOperation with that request. It can make things easier and straight forward.

For your next question: Not all the objects are valid JSOn objects and you only can pass valid JSON objects with request body otherwise server side might not be able to decrypt it(Server side will not be aware of your custom object).

2
Wain On

Your code looks ok. The problem appears to be that you aren't setting the request serialisation type to JSON so you are getting a form URL encoded body.