RestKit ios - put - json instead of form encoded RestKit ios - put - json instead of form encoded json json

RestKit ios - put - json instead of form encoded


The easiest way is to simply set the property when you initialize the object manager, like so:

RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://url.com"];objectManager.serializationMIMEType = RKMIMETypeJSON;


Evan is correct, but I've had to also make sure I am sending a JSON string, because I had a nested NSDictionay.

If you have a dictionary you want to send as a JSON string, here's how you can do it:

// create a JSON string from your NSDictionaryNSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict                                                    options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string                                                     error:&error];NSString *jsonString = [[NSString alloc] init];if (!jsonData) {    NSLog(@"Got an error: %@", error);} else {    jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];}// make the post using the objectManager if you want to map the response to a modelRKObjectManager* objectManager = [RKObjectManager sharedManager];  [objectManager loadObjectsAtResourcePath:@"/api/" delegate:self block:^(RKObjectLoader* loader) {    loader.serializationMIMEType = RKMIMETypeJSON; // We want to send this request as JSON    loader.objectMapping = [objectManager.mappingProvider objectMappingForClass:[Plan class]];    loader.resourcePath = @"/api/";    loader.method = RKRequestMethodPOST;    loader.params = [RKRequestSerialization serializationWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] MIMEType:RKMIMETypeJSON];}];


Okay just found how to do it:

subclass RKRouter.h or just change in RKDynamicRouter.m

return [object propertiesForSerialization];

to

[RKJSONSerialization JSONSerializationWithObject:[object propertiesForSerialization]];

and RestKit generate JSON for putObject call