Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: RestKit loadObject & send params

using loadObjectAtResourcePath on GET method, doesn't include my parameters on the requests.

for example, if I send:

[RKObjectManager objectManagerWithBaseURL:@"http://something/ws"];
    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"/res" delegate:self block:^(RKObjectLoader *loader) {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                              @"val", @"param1",
                              nil];
        loader.params = [RKParams paramsWithDictionary:dict];
    }];

the final url request doesn't include the "?param1=val" part - why?

like image 279
Alon Amir Avatar asked Feb 22 '12 21:02

Alon Amir


1 Answers

Update Months Later

The real answer is that loader.params creates the HTTP BODY, hence it works for POST, PUT, DELETE etc but not for GET where the params are appended to the URL.

Hence, the answer below still works if you're facing the same issue for GET, but if you're sending out GET requests, it's mostly using methods that attach the params to the query string.

To summarize the differences between the two.

Sending params in the HTTP Body(i.e. POST, UPDATE, DELETE)

// Convert a NS Dictionary into Params
RKParams *params = [RKParams paramsWithDictionary:optionValues];

// I use sendObject to skip the router. Otherwise it's normally postObject
[[RKObjectManager sharedManager] sendObject:yourObject toResourcePath: yourResourcePath usingBlock:^(RKObjectLoader *loader) {
    loader.method = RKRequestMethodPOST;
    loader.delegate = delegate;
    loader.params = params; // This sets params in the POST body and discards your yourObject mapping

} ];

Caveat Emptor (for above)

Setting params in the block destroys any mapping that you might have set in yourObject, kind of defeats the purpose of using object mapping. There's a fix here by Sebastian loader.params - Extra params if you really want to use this method to append extra parameters to your Post not in the object.

Sending in params as Query String (i.e. GET)

// Make a NS dictionary and use stringByAppendingQueryParameters
NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];

[[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json" stringByAppendingQueryParameters:shopParams] delegate:objectDelegate];

The rest of the answer is just for reference, I'm a hoarder.


Old Answer

I'm using RestKit for my project and facing the same issue.

I think RKParams is mainly used to do POST requests. I cannot fully decipher your code because 1) I don't know loader's declaration? 2) RKParams is not to be used with Object Manager?

I did this.

Loader Method in App Delegate

NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:@"limit",@"30", nil]; 

[[RKClient sharedClient] get:@"/api/v1/shops.json" queryParams:shopParams delegate:self];

Delegate

- (void)requestDidStartLoad:(RKRequest *)request {
    NSLog(@"RK Request description: %@",[request description]);
}

Output: RK Request description: <RKRequest: 0x7993db0> and rails log say {"limit"=>"30"}.

From the autocomplete in Xcode, you can see the get request didn't even use RKParams. Just a NSDict. The POST requests uses it.

My goal is to attach a query string, i.e. ?location=singapore&etcetc to my API methods in Rails. For this, RK comes with a NSString addon called appendQueryParams RK docs link that you can use to append query params.

If your goal is POST images etc, you can follow the above line of thought of using RKClient.

Update:

If you just want to append parameters to Object Manager

 NSDictionary *shopParams = [NSDictionary dictionaryWithKeysAndObjects:
                                @"limit",@"20", 
                                @"location",@"latitude,longitude",
                                nil];

This is outdated and marked for deprecation.


 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json" appendQueryParams:shopParams] delegate:self];

Use this instead:

 [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/v1/shops.json stringByAppendingQueryParameters:shopParams] delegate:yourLoaderDelegate];

Rails Log: {"location"=>"latitude,longitude", "limit"=>"20"}

Hope in my answer I didn't make any wrong statements.

Refer to this question RestKit GET query parameters.

like image 86
Damon Aw Avatar answered Sep 22 '22 03:09

Damon Aw