Note that there are some explanatory texts on larger screens.

plurals
  1. PORestKit object mapping request that has nested JSON in its HTTP body
    primarykey
    data
    text
    <p>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:</p> <pre><code>@interface RequestObject : NSObject @property (nonatomic, copy) NSString *title; @property (nonatomic, strong) NSDictionary *location; @property (nonatomic, strong) NSMutableArray *pictures; </code></pre> <p>I want to map my object such that in its HTTP Body, the JSON would look like this:</p> <pre><code>{ "title": "Magic School Bus", "location": { "latitude": "38.764792", "longitude": "-121.247574" }, "pictures": [ { "base64pic": "iVBORw0KGg..." } ] } </code></pre> <p>For the request object, I configured it like so:</p> <pre><code>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]; </code></pre> <p>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:</p> <pre><code>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) { </code></pre> <p>When I make the POST call in my project, I see this as the HTTP Request body in my console:</p> <pre><code>Request Body : location[latitude]=38.757965&amp;location[longitude]=-121.254044&amp;pictures[][base64pic]=somebase64string&amp;title=MyTitle </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload