Restkit Request Not Sending Parameters

Restkit request not sending parameters

Ensure that you have set:

manager.requestSerializationMIMEType = RKMIMETypeJSON;

As the default is RKMIMETypeFormURLEncoded.


Based on your comments and discussion, the mapping isn't being used because the class of the user is wrong. This is because it is a plain NSManagedObject rather than an RKUser instance. This means RestKit can't find the appropriate mapping. The cause for this is the custom class not being set on the User entity in your Core Data model.

Restkit Post Not sending Object

I was dealing with the same problem. You are missing request mapping. You're only describing response.

Example code:

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userMapping 
objectClass:[User class] rootKeyPath:nil];

[objectManager addRequestDescriptor: requestDescriptor];

Can't get RestKit 0.2 to POST parameters

Finally figure it out, and of course it was a really stupid issue. The server was expecting a dictionary of params, but my object manager's requestSerializationMIMEType was set to RKMIMETypeJSON. So, once I commented that line out the request worked fine with the object being nil and the parameters being set to a dictionary of @{@"email": email, @"password": password}

Exclude some parameters with POST request on RestKit

You would need to use RestKit's Dynamic mapping class to handle mapping at run time.

Dynamic Object Mapping

RestKit supports such use cases via the RKDynamicMapping class.
RKDynamicMapping is a sibling class to RKObjectMapping and can be
added to RKRequestDescriptor and RKResponseDescriptor objects and used
to configure RKMappingOperation instances. RKDynamicMapping allows you
to hook into the mapping process and determine an appropriate concrete
RKObjectMapping to use on a per-object basis.

Or you could not use RestKit and set the POST body yourself. Create the required dictionary by adding only required parameters. Serialize this object with help of NSJSONSerialization and set this NSData object as HTTP Body in the request instance.

RestKit GET query parameters

  1. Why doesn't the second solution work?

params is used to create a HTTP body, which is not used in a GET/HEAD request.

  1. Why is the first solution working without having to set the loader.targetObject to nil, although I do not have any root key path
    in the JSON response?

I think targetObject is nil by default. You normally don't set it, the request will create it if needed. The only time I use it is when requesting objects without primary keys or other weird problems.

  1. What are the cases where I should use the getObject:usingBlock method? What is its intended purpose?

This is a convenience method so you don't have to remember all the correct syntax. Internally it just sends an object load request using GET.

EDIT:

Use this if you have an object you want to update.

  1. What should I use loader.params for? The object mapping tutorial from the wiki says this property can be used to encapsulate POST
    parameters, but I do not see the point since I can wrap the parameters
    in the serialized object that is being sent with the method
    postObject:usingBlock.

Whatever you put in params will be serialized to an HTTP body (or body stream). Again, postObject:usingBlock: is just a convenience method so you don't have to remember everything.

RestKit is open source. If you are not sure how it works you are free to follow the parameters internally. If you app and web service is well designed, you should be able to use the convenience methods. Sometimes you can not, and then you can use the raw forms like you have done.

EDIT:
Q Hrm, quoting your bullet points messed up the numbers...

iOS: RestKit loadObject & send params

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.

iOS - RESTKit 0.20 - send multiple query parameters with the same name

I figured out a solution. This is the code I use:

[[RKObjectManager sharedManager] getObjectsAtPath:queryPath
parameters:nil
success:nil failure:nil];

and the queryPath looks like this: getNames?names=bob&names=joe&names=joey and it actually works.
make sure to use stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding so that the & and ? will go through properly .



Related Topics



Leave a reply



Submit