1

I am new in RestKit. I did not find a proper documentation or tutorial to send simple object data to Restful API.

Here is my problem in detail.

I have a class with name User having two properties for now: email and password. I want to send them to server using RestKit 0.20.

I found some tutorials but all of them are outdated for RestKit v 0.10. I found this question but this is outdated as well. There is no sharedInstance selector of class RKObjectManager in RestKit 0.20 but sharedManager.

Any help would be great.

Community
  • 1
  • 1
regeint
  • 888
  • 2
  • 17
  • 38
  • check this: http://restkit.org/api/latest/Classes/RKObjectManager.html – Mateusz Apr 09 '14 at 18:07
  • The method is really `sharedManager`... – Wain Apr 09 '14 at 18:08
  • @Mateusz this document is outdated as well because it says: requestDescriptorWithMapping:objectClass:rootKeyPath is deprecated – regeint Apr 09 '14 at 18:23
  • @regeint that right, but if you search deeper you will find: `This method is deprecated. Use + (instancetype)requestDescriptorWithMapping:(RKMapping *)mapping objectClass:(Class)objectClass rootKeyPath:(NSString *)rootKeyPath method:(RKRequestMethod)method instead.` [source](http://restkit.org/api/0.20.3/Classes/RKRequestDescriptor.html#//api/name/requestDescriptorWithMapping:objectClass:rootKeyPath:) – Mateusz Apr 10 '14 at 06:51

1 Answers1

1

Finally I found the solution. Thanks @Mateusz for helping me out. Here is the solution.

// Construct a request mapping for User
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:@{ @"email": @"email", @"password": @"password" }];


// construct a response mapping for User
RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[User class]];
[responseMapping addAttributeMappingsFromDictionary:@{@"email": @"email", @"password": @"password", @"guid": @"guid"}];


RKRequestDescriptor *req = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[User class] rootKeyPath:@"user" method:RKRequestMethodPOST];

RKResponseDescriptor *res = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"user" statusCodes:[NSIndexSet indexSetWithIndex:200]];

// Register our descriptors with a manager
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost/api/user/"]];

[manager addRequestDescriptor:req];
[manager addResponseDescriptor:res];

// preparing sending User object
User *user = [User new];
user.email = @"example@example.com";
user.password = @"password";

NSLog(@"user email : %@", user.email);

[manager postObject:user path:@"user" parameters:@{@"api_key": MY_API_KEY} success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
    NSArray *arr = [result array];
    User *temp= [arr objectAtIndex:0];
    NSLog(@"SUCCESS ---------------------------- User's email: %@", temp.email);
    NSLog(@"User's guid: %@", temp.guid);

    //                                NSLog(@"--------- - --- -- - all resutl: %@", result);
}
            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                NSLog(@"failed post %@", error);
                NSLog(@"%@",operation.description);
                NSLog(@"%@",operation.HTTPRequestOperation.description);
            }];
regeint
  • 888
  • 2
  • 17
  • 38