Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit 0.20 JSON object is being serialized as GET style request in POST body

Tags:

json

ios

restkit

I'm just starting out with RestKit 0.20.0 and I'm having trouble creating a nicely formatted JSON request.

I get this (from rest kit logs):

request.body=title=A%20glorious%20walk%20in%20the%20woods&startDateTime=2013-01-13%2016%3A09%3A33%20%2B0000&endDateTime=2013-01-13%2016%3A09%3A43%20%2B0000&points[][longitude]=-122.0307725&points[][latitude]=37.3310798&points[][longitude]=-122.0307334&points[][latitude]=37.33154242&points[][longitude]=-122.03075743&points[][latitude]=37.33138305&points[][longitude]=-122.03075659&points[][latitude]=37.33131185&points[][longitude]=-122.03057969&points[][latitude]=37.33156519&points[][longitude]=-122.03075535&points[][latitude]=37.33144466&points[][longitude]=-122.03076342&points[][latitude]=37.33123666&points[][longitude]=-122.03074488&points[][latitude]=37.33149482&points[][longitude]=-122.03068145&points[][latitude]=37.33155419&points[][longitude]=-122.03062909&points[][latitude]=37.33156564&points[][longitude]=-122.03076853&points[][latitude]=37.33115792

when I want this (normal json object with curly brackets and an array for the points property):

{
    title: "Something",
    startDateTime: "dateinfo",
    endDateTime: "moredateinfo",
    points: [
        {
            latitude: "37.33131313",
            longitude: "122.4325454"
        },
        {
            latitude: "37.33131313",
            longitude: "122.4325454"
        }
    ]
}

I have two main objects: A DLWalk than contains an NSSet of DLPoint objects (They are CoreData objects, but at the moment I am ignoring that and just focusing on creating an HTTP Request)

Here's the code I'm using to create my request:

// Point mapping
RKObjectMapping *mappingPoint = [RKObjectMapping requestMapping];
[mappingPoint addAttributeMappingsFromArray:@[@"latitude", @"longitude"]];
RKRequestDescriptor *reqDescPoint = [RKRequestDescriptor requestDescriptorWithMapping:mappingPoint objectClass:[DLPoint class] rootKeyPath:nil];

// Walk mapping
RKObjectMapping *mappingWalk = [RKObjectMapping requestMapping];
[mappingWalk addAttributeMappingsFromArray:@[@"endDateTime", @"startDateTime", @"title"]];
RKRequestDescriptor *reqDescWalk = [RKRequestDescriptor requestDescriptorWithMapping:mappingWalk objectClass:[DLWalk class] rootKeyPath:nil];


// Define the relationship mapping
[mappingWalk addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"points" toKeyPath:@"points" withMapping:mappingPoint]];

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://192.168.1.10:8080"]];
                            [manager addRequestDescriptor:reqDescWalk];
                            [manager addRequestDescriptor:reqDescPoint];
                            [manager addResponseDescriptor:responseDescriptor];

// POST to create
[manager postObject:walk path:@"/walk/save" parameters:nil success:nil failure:nil];

So the question is: why am I not getting a normal looking JSON object in my POST body?

like image 271
codemonkey Avatar asked Jan 17 '13 10:01

codemonkey


1 Answers

What you get as request.body is URL-encoded, which is RESTKit default behaviour and usually works fine.

If you want it to be JSON-encoded, just insert this line before posting the query

manager.requestSerializationMIMEType=RKMIMETypeJSON;

For more information on this, have a look there in the API documentation for RKObjectManager class : requestSerializationMIMEType

like image 76
JD_ Avatar answered Oct 01 '22 03:10

JD_