RestOK PostObject with request parameters

I am using RestKit and I am trying to publish an object with a request parameter (auth token in the form token=<token> ), but I cannot figure out how to make it work. That's what I'm doing...

First, I add the mapping of the request object to the manager:

 RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; [requestMapping addAttributeMappingsFromDictionary:@{ @"id" : @"id", @"name" : @"name", @"latitude" : @"latitude", @"longitude" : @"longitude" }]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[Location class] rootKeyPath:nil]; [manager addRequestDescriptor:requestDescriptor]; 

Then I make a request:

 RKManagedObjectRequestOperation *operation = [RKObjectManager.sharedManager appropriateObjectRequestOperationWithObject:self method:RKRequestMethodPOST path:@"/api/v1/users/3/locations" parameters:@{@"token" : token}]; [operation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { Location * location = (Location*)mappingResult; self.id = Location.id; } failure:^(RKObjectRequestOperation *operation, NSError *error) { ALog(@"fail!"); }]; [RKObjectManager.sharedManager enqueueObjectRequestOperation:operation]; 

When a request is made, the Location object is serialized in JSON and completely placed in the request body. However, instead of the token being added to the query string, it is added as JSON to the request body.

Example:

 request.body={"id":0,name="test","longitude":-0.1337,"latitude":51.50998,"token":"Z3JlZ2c6MTM2MDU2OTk2MDY2OTpMajkxd01acWxjcGg1dEpFVy9IaEcwNTcyMWJkSEpnTFRTQTI2eXNlN29VOVRTc1UwV1lEU0E9PQ=="} 

Any help is much appreciated!

+4
source share
3 answers

There is a Gist at https://gist.github.com/onelittlefish/5970616 , which provides a good extension to RKObjectManager , which allows you to add request parameters to PUT or POST.

Just drop these files into your project, import the header, and you can use something similar to the answer by @giuseppe (which adds params to the body, not the path). The only difference is changing the parameters to queryParameters - your call might look something like this:

 [objectManager postObject:self path:@"/api/v1/users/3/locations" queryParameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { Location * location = (Location*)mappingResult; self.id = Location.id; } failure:^(RKObjectRequestOperation *operation, NSError *error) { ALog(@"fail!"); } ]; 
+1
source

In my implementation, I added request parameters in the URL itself:

 RKManagedObjectRequestOperation *operation = [RKObjectManager.sharedManager appropriateObjectRequestOperationWithObject:self method:RKRequestMethodPOST path:[NSString stringWithFormat:@"/api/v1/users/3/locations?token=%@",token] parameters:nil]; 
0
source

As easy as reading the many tutorials available on the Internet. But:

 NSDictionary *queryParams; queryParams = [NSDictionary dictionaryWithObjectsAndKeys: token, @"token",nil]; RKResponseDescriptor *tokenResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:loginMapping pathPattern:nil keyPath:@"yourpathtoyoyrkey" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; [objectManager addResponseDescriptor:tokenResponseDescriptor]; objectManager.requestSerializationMIMEType = RKMIMETypeJSON; [objectManager postObject:loginMapping path:@"yourmethod.json" parameters:queryParams success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { } failure:^(RKObjectRequestOperation *operation, NSError *error) { //NSLog(@"Error WS RK:%@",error.localizedDescription); } ]; 
0
source

All Articles