4

I am trying to migrate to RestKit 0.20-pre2.

Currently I managed to migrate my mapping (at least the compiler does not complain anymore), but I have problems in creating requests (previously I used the RKObjectLoader which does not exist anymore.

My previous code is the following:

- (RKObjectLoader*)objectLoaderWithResourcePath: (NSString*)resourcePath
                                     method: (RKRequestMethod)httpMethod
                                 parameters: (NSDictionary*)parameters
                              mappableClass: (Class)objectClass
{
RKObjectMapping *mapping = [self.objectManager.mappingProvider     objectMappingForClass:objectClass];

NSString *path = resourcePath;
if (httpMethod == RKRequestMethodGET) {
    path = [resourcePath stringByAppendingQueryParameters:parameters];
}
RKObjectLoader *request = [self.objectManager loaderWithResourcePath:path];
request.method = httpMethod;
request.delegate = self;
request.objectMapping = mapping;
if (httpMethod != RKRequestMethodGET) {
    request.params = parameters;
}

return request;
}

I used the above method to create a generic request, and then send it either synchronously or asynchronously. Now... I saw the new method getObjectsAtPath: parameters: success: failure:, but.. I need the same for the POST (and I don't have any object to post... it is simply the server which accept a POST request for the login..)

Any help?

Thank you

David
  • 9,635
  • 5
  • 62
  • 68
Francesco
  • 1,840
  • 19
  • 24

2 Answers2

2

I had the same problem as you and i received a great answer here:

Trying to make a POST request with RestKit and map the response to Core Data

Basically,

This is what you need:

 NSDictionary *dictionary = @{ @"firstParam": @(12345), @"secondParam": @"whatever"};
 NSMutableURLRequest *request = [objectManager requestWithObject:nil     method:RKRequestMethodPOST path:@"/whatever" parameters:parameters];

 RKObjectRequestOperation *operation = [objectManager  objectRequestOperationWithRequest:request ^(RKObjectRequestOperation *operation,     RKMappingResult *result) {
NSLog(@"Loading mapping result: %@", result);
  } failure:nil];

There is an example here in README section Managed Object Request that will help you:

https://github.com/RestKit/RestKit

Community
  • 1
  • 1
Vlad Bogdan
  • 700
  • 1
  • 8
  • 25
  • thank you! I temporary solved by using AFNetworking url request functions, but your answer is what I really needed! I thought I had to use the object parameter as in the post method, but now I see it can stay nil! Thank you – Francesco Dec 27 '12 at 11:09
1

You can use AFNetworking directly using the RK HTTPClient subclass, something like this:

[[RKObjectManager sharedManager].HTTPClient postPath:@"/auth" parameters:params success:^(AFHTTPRequestOperation *operation, id JSON)
 {
     // Success
 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     // Error
 }];

Since RestKit v0.20.x, RK now use AFNetworking under the hood instead of RKClient, so you can refer directly to the AFNetworking docs:

http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/postPath:parameters:success:failure:

Edit

In my project, for the auth, I simply created an NSObject named User, with a singleton, and managed the mapping myself. I (personally) didn't need to have my auth user in my core data stack. If you need to use the RK Core data mapping capabilities, take a look at RKObjectManager with the postObject:path:parameters:success:failure: method.

allaire
  • 5,995
  • 3
  • 41
  • 56
  • But... I need mapping too... The result of my post action is a User object... Any hint on this? – Francesco Dec 08 '12 at 08:41
  • I will think about the "manually mapped" user object. The reason because I haven't already done this is because there are a lot of user in the application, and the "Login" user is simply one of those... btw: postObject:path:etc.. wants an object (must not be nil)... so..which object I use if I want to retrieve it? – Francesco Dec 19 '12 at 08:49