Restkit Post Not sending Object

dostrander picture dostrander · Dec 19, 2012 · Viewed 8.9k times · Source

I am creating an IOS app that uses restkit and I am able to do GET requests no problem and it maps correctly and everything however, I am having trouble getting restkit to do a POST to the database. It is getting a response that is saying:

`{"errors":{"email":["can't be blank"],"password":["can't be blank"]}}`

I have a feeling that the object is just not being sent. Here is my code to set up all the mappings, in my app delegate.

RKObjectMapping *userMapping     = [RKObjectMapping mappingForClass:[User       class]];
RKObjectMapping *dogMapping      = [RKObjectMapping mappingForClass:[Dog        class]];
RKObjectMapping *posttimeMapping = [RKObjectMapping mappingForClass:[PostTime   class]];
RKObjectMapping *postMapping     = [RKObjectMapping mappingForClass:[Post       class]];
RKObjectMapping *activityMapping = [RKObjectMapping mappingForClass:[Activity   class]];

        // set up which attributes go where
[userMapping        addAttributeMappingsFromDictionary:[User propertyMappings]];
[dogMapping         addAttributeMappingsFromDictionary:[Dog propertyMappings]];
[posttimeMapping    addAttributeMappingsFromDictionary:[PostTime propertyMappings]];
[postMapping        addAttributeMappingsFromDictionary:[Post propertyMappings]];
[activityMapping    addAttributeMappingsFromDictionary:[Activity propertyMappings]];

        // set up relationships
[userMapping        addRelationshipMappingWithSourceKeyPath:@"dogs"         mapping:dogMapping];
[dogMapping         addRelationshipMappingWithSourceKeyPath:@"timeline"     mapping:posttimeMapping];
[posttimeMapping    addRelationshipMappingWithSourceKeyPath:@"posts"        mapping:postMapping];
[postMapping        addRelationshipMappingWithSourceKeyPath:@"activities"   mapping:activityMapping];


    // what to print
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
RKLogConfigureByName("Restkit/Network", RKLogLevelDebug);



    // set up paths
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:3000"]];

NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2x
RKResponseDescriptor *userDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern: @"/users/1.json" keyPath:@"" statusCodes:statusCodes];
RKResponseDescriptor *signupDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:userMapping pathPattern:@"/users.json" keyPath:@"" statusCodes:statusCodes];
[manager addResponseDescriptorsFromArray:@[ userDescriptor, signupDescriptor ]];
[RKObjectManager setSharedManager:manager];

and here is my post request:

- (void) post{
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager postObject:self path:@"/users.json" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"successful post");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"failed post %@", error);
    NSLog(@"%@",operation.description);
    NSLog(@"%@",operation.HTTPRequestOperation.description);
}];

}

Here are the logs I am getting:

2012-12-19 11:23:15.657 Dogapp[6353:4303] T restkit.network:RKHTTPRequestOperation.m:139     POST 'http://localhost:3000/users.json':
 request.headers={
 Accept = "application/json";
"Accept-Language" = "en, fr, de, ja, nl, it, es, pt, pt-PT, da, fi, nb, sv, ko, zh-Hans, zh-Hant, ru, pl, tr, uk, ar, hr, cs, el, he, ro, sk, th, id, ms, en-GB, ca, hu, vi, en-us;q=0.8";
"Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";
"User-Agent" = "Dogapp/1.0 (iPhone Simulator; iOS 6.0; Scale/1.00)";
}request.body=(null)
2012-12-19 11:23:15.680 Dogapp[6353:190b] T restkit.network:RKHTTPRequestOperation.m:156     POST 'http://localhost:3000/users.json' (422):  
response.headers={
"Cache-Control" = "no-cache";
Connection = "Keep-Alive";
"Content-Length" = 69;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 19 Dec 2012 16:23:15 GMT";
Server = "WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)";
"Set-Cookie" =     "_cms_session=BAh7BkkiD3Nlc3Npb25faWQGOgZFRkkiJTRjMTdjZDNiOGVmNDY0MGFjY2NiZWJjNTUwOGQxZDU3BjsAVA%3D%3D--d553a2616530bbe5346fdafc4d6fca81ecc6f6f7; path=/; HttpOnly";
"X-Request-Id" = 5edef73ca7b5ddbc4e510e9799835324;
"X-Runtime" = "0.007666";
"X-Ua-Compatible" = "IE=Edge";
}
response.body={"errors":{"email":["can't be blank"],"password":["can't be blank"]}}
2012-12-19 11:23:15.682 Dogapp[6353:c07] failed post Error   Domain=org.restkit.RestKit.ErrorDomain Code=1001 "Unable to find any mappings for the given    content" UserInfo=0x95bcb20 {DetailedErrors=(
), NSLocalizedDescription=Unable to find any mappings for the given content, keyPath=null}
2012-12-19 11:23:15.683 Dogapp[6353:c07] mappingResult: <RKObjectRequestOperation:  0x75e73b0, state: Failed, isCancelled=NO, request: <NSMutableURLRequest    http://localhost:3000/users.json>, response: <NSHTTPURLResponse: 0x75f4580 statusCode=422  MIMEType=application/json length=69>>
2012-12-19 11:23:15.683 Dogapp[6353:c07] <RKObjectRequestOperation: 0x75e73b0, state: Failed, isCancelled=NO, request: <NSMutableURLRequest http://localhost:3000/users.json>, response: <NSHTTPURLResponse: 0x75f4580 statusCode=422 MIMEType=application/json length=69>>
2012-12-19 11:23:15.683 Dogapp[6353:c07] <RKHTTPRequestOperation: 0x75e78e0, state: isFinished, cancelled: NO request: <NSMutableURLRequest http://localhost:3000/users.json>, response: <NSHTTPURLResponse: 0x75f4580>>

Any example or point in the right direction would be amazing. I have been stuck on this for a while.

If you need any more information just let me know.

Update

I was able to get it to work but not the correct way.Basically I just send all of the information through the params like so.

- (void) post{
RKObjectManager *manager = [RKObjectManager sharedManager];
[manager postObject:self path:@"/users.json" parameters:@{@"user[email]" : self.email, @"user[password]" : self.password}  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Successful Post!");
    [User setCurrentUser:self];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"Okay!" otherButtonTitles: nil];
    [alert show];
}];

}

Anyone have any ideas to how to do it correctly?

Update for Vishal For using coredata you have to create an Managed Object Store like this...

 RKObjectManager *manager = [RKObjectManager managerWithBaseURL:    [NSURL URLWithString:@"http://my_url.com"]];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyDataModel" withExtension:@"momd"];
NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
RKManagedObjectStore *managedObjectStore = [[RKManagedObjectStore alloc] initWithManagedObjectModel:managedObjectModel];
manager.managedObjectStore = managedObjectStore;

Then you're going to have to use RKEntityMapping instead of RKObjectMapping

Hope this helps.

Update 2 for Vishal

[managedObjectStore createPersistentStoreCoordinator];
NSError *error;

BOOL success = RKEnsureDirectoryExistsAtPath(RKApplicationDataDirectory(), &error);
if (! success) {
    RKLogError(@"Failed to create Application Data Directory at path '%@': %@", RKApplicationDataDirectory(), error);
}
NSString *path = [RKApplicationDataDirectory() stringByAppendingPathComponent:@"Store.sqlite"];
NSPersistentStore *persistentStore = [managedObjectStore addSQLitePersistentStoreAtPath:path fromSeedDatabaseAtPath:nil withConfiguration:nil options:nil error:&error];
if (! persistentStore) {
    RKLogError(@"Failed adding persistent store at path '%@': %@", path, error);
}

[managedObjectStore createManagedObjectContexts];
managedObjectStore.managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];

Answer

juanignaciosl picture juanignaciosl · Dec 22, 2012

I was dealing with the same problem. You are missing request mapping. You're only describing response.

Example code:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping 
    objectClass:[User class] rootKeyPath:nil];

[objectManager addRequestDescriptor: requestDescriptor];