Reputation: 915
I'm using RestKit to post an object to a server or to get it. GET works great with mapping. But POST doesn't.
Here is the form of the received JSON :
{"user":{"id":98,"notification":false,"id":98,"token_session":"506934689"}}
the problème is when a post object the sent JSON has this form :
{"id":98,"notification":false,"id":98,"token_session":"506934689"}
as you can see, "user" class name is missing! (I can't change serveur API to work with both JSON format)
here is my code if anyone can help me !
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[UserObject class]];
[mapping mapKeyPath:@"id" toAttribute:@"id"];
[mapping mapKeyPath:@"token_session" toAttribute:@"token_session"];
[mapping mapKeyPath:@"notification" toAttribute:@"notification"];
[[RKObjectManager sharedManager].mappingProvider setMapping:mapping forKeyPath:@"user"];
[[RKObjectManager sharedManager].mappingProvider registerMapping:mapping withRootKeyPath:@"user"];
[[RKObjectManager sharedManager].mappingProvider setSerializationMapping:[mapping inverseMapping] forClass:[UserObject class]];
//Route :
[[RKObjectManager sharedManager].router routeClass:[UserObject class] toResourcePath:@"user"];
And the post of the object :
RKObjectManager * manager = [RKObjectManager sharedManager];
[manager postObject:user usingBlock:^(RKObjectLoader* loader)
{
loader.delegate = self;
}];
Thanks a lot.
EDIT :
I tried this :
RKObjectManager * manager = [RKObjectManager sharedManager];
[manager postObject:[CurrentSession sharedInstance].user usingBlock:^(RKObjectLoader* loader)
{
loader.sourceObject = [NSDictionary dictionaryWithObject:[CurrentSession sharedInstance].user forKey:@"user"];
loader.resourcePath = url;
loader.delegate = self;
}];
but the JSON so send is empty :(
Upvotes: 0
Views: 437
Reputation: 915
I found my mistake !
[[RKObjectManager sharedManager].mappingProvider setMapping:mapping forKeyPath:@"user"];
[[RKObjectManager sharedManager].mappingProvider registerMapping:mapping withRootKeyPath:@"user"];
RestKit doesn't seems to like those both line. I kept only the first line and the send JSON became what I expected
{"user":{"id":98,"notification":false,"id":98,"token_session":"506934689"}}
Upvotes: 1