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
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.