Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit ios - put - json instead of form encoded

i am writing an ios app that uses restkit to communicate with a web server through Rest with JSON

i am able to use [[RKObjectManager sharedManager] loadObjectsAtResourcePath:path delegate:self] to get object from my web service as JSON, map it to obj-c object, it works fine

now i am trying to use: [[RKObjectManager sharedManager] putObject:obj delegate:self]; and this call sends an object to the web service as form encoded and not JSON

so my question is: how to configure the sharedManager (or the routeur?) to send with content type JSON instead of form encoded.

any code example much appreciated.

Thx!

like image 924
goane Avatar asked Mar 16 '11 01:03

goane


4 Answers

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;
like image 128
Evan Cordell Avatar answered Oct 17 '22 15:10

Evan Cordell


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 NSDictionary
NSError *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 model
RKObjectManager* 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];
}];
like image 27
Flaviu Avatar answered Oct 17 '22 17:10

Flaviu


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

like image 32
goane Avatar answered Oct 17 '22 16:10

goane


Create an Object Manager and set the property for matching the header in JSON format

RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://mobile.com"]];
[objectManager addResponseDescriptorsFromArray:@[responseDescriptor]];

objectManager.requestSerializationMIMEType = RKMIMETypeJSON;
like image 1
Jesús Hurtado Avatar answered Oct 17 '22 16:10

Jesús Hurtado