Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit - POST response object mapping

I have method "getBuses" on my RESTful webservice (Rails). That method needs to return me list of buses in JSON. But, when I sending request for "/getBuses.json" I need to send

params = [NSDictionary 
dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", 
@"fromSerbia", nil]; 

I'm posting params because I need filtered response (just for specific tour on specific date). Then, I expecting response like this:

{ 
  "buses" : [ { 
    "bus_number" : "1", 
    "created_at" : "2011-08-15T23:07:52Z", 
    "id" : 1, 
    "lat" : 44.815, 
    "long" : 20.4665, 
    "model" : "Setra", 
    "registar_number" : "123456", 
    "seats" : 50, 
    "tour_id" : 1, 
    "updated_at" : "2011-08-15T23:07:52Z" 
  }, { 
    "bus_number" : "2", 
    "created_at" : "2011-08-15T23:07:52Z", 
    "id" : 2, 
    "lat" : 44.812, 
    "long" : 20.465, 
    "model" : "Mercedes", 
    "registar_number" : "2234", 
    "seats" : 60, 
    "tour_id" : 1, 
    "updated_at" : "2011-08-15T23:07:52Z" 
  } ] 
} 

I just need NSArray with Bus objects. Can you give me code example how to make this request and make object mapping with RestKit?

like image 221
dormitkon Avatar asked Jul 04 '26 04:07

dormitkon


1 Answers

As I told you in the IRC Channel, in your RKClient instance you can use this method:

(NSURL *)URLForResourcePath:(NSString *)resourcePath queryParams:(NSDictionary *)queryParams

So you should do it like this:

[objectManager.mappingProvider setMapping:busesMapping forKeyPath:@"buses"];

params = [NSDictionary dictionaryWithObjectsAndKeys: @"2011-08-16", @"datum", @"TRUE", @"fromSerbia", nil];
NSURL *someURL = [objectManager.client URLForResourcePath:@"/service/getLocations.json" queryParams:params];

[objectManager loadObjectsAtResourcePath:[someURL absoluteString] delegate:self];

That method builds a valid URL using the resourcePath and the parameters you give as parameter. And then you need to set it back using the absoluteString of the NSURL object. Hope this helps!

like image 95
rodchile Avatar answered Jul 05 '26 18:07

rodchile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!