Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit

Right now I’m doing a huge refactory to my code and I want to unify the way I’m using Restkit. I had separated ways of making RPC calls to my API server and REST calls.

REST calls where made using objectMapping and RPC calls where made with the RKClient. Besides that, I’m using blocks instead of delegates, which is great but I have some doubts about how it works.

  • PayPal API : How to integrate it in my App? IOS5
  • Wait until animation block finishes before segue
  • Why do we pass self in @synchronized block?
  • iOS facebook check if friends have app installed
  • Pass value to closure?
  • Swift blocks not working
  • Here is the code I had before, which worked nicely to post the object and do the mapping manually using delegates and after that is the new code using blocks that does not send the params.

    //This was the old way...
    - (void) upload: (KFMedia *) pic {
    
        RKParams* imageParams = [RKParams params];
        NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
        [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
    
        [[RKClient sharedClient] post:@"/api/upload/" params:imageParams delegate:self]; 
    
    }
    
    //This is the new way I'm trying...
    - (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{
    
        RKParams* imageParams = [RKParams params];
        NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
        [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
    
        [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/upload/" usingBlock:^(RKObjectLoader *loader) {
    
            //Trying to set params here, but it seems that I'm not sending anything :(
            loader.params = imageParams;
            loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];
            loader.delegate = self;
            loader.onDidLoadObject = loadBlock;
            loader.onDidFailWithError = failBlock;
            loader.onDidFailLoadWithError = failBlock;
            loader.onDidLoadResponse = ^(RKResponse *response) {
                [self fireErrorBlock:failBlock onErrorInResponse:response];
            };
    
        }];
    
    }
    

    The request I’m sending has its body empty, that means that the params are not being sendend or setted properly. Any ideas on how to get this to work?

    Solutions Collect From Internet About “Making a post to API with RKParams and mapping the response with RKObjectMapping using RestKit”

    I just solved the problem. To send the extra params with loadObjectsAtResourcePath you must force a post using

    loader.method = RKRequestMethodPOST;
    

    the code is as follows:

    - (void) upload: (KFMedia *) pic onLoad:(RKObjectLoaderDidLoadObjectBlock) loadBlock onError:(RKRequestDidFailLoadWithErrorBlock)failBlock{
    
        RKParams* imageParams = [RKParams params];
        NSData* imageData = UIImageJPEGRepresentation(pic.image, 0.7f);
        [imageParams setData:imageData MIMEType:@"image/jpg" forParam:@"FileUpload"];
    
        [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/api/upload/" usingBlock:^(RKObjectLoader *loader) {
    
            loader.method = RKRequestMethodPOST; //This line solved the problem
            loader.params = imageParams;
            loader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[KFMedia class]];
            loader.delegate = self;
            loader.onDidLoadObject = loadBlock;
            loader.onDidFailWithError = failBlock;
            loader.onDidFailLoadWithError = failBlock;
            loader.onDidLoadResponse = ^(RKResponse *response) {
                [self fireErrorBlock:failBlock onErrorInResponse:response];
            };
    
        }];
    
    }