4
votes

I'm using RestKit and I'm trying to post an object with a query parameter (an auth token in the form of token=<token>), but I can't figure out how to get it to work. Here's what I'm doing...

First, I add the request object mapping 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 the 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 the request is made, the Location object is serialized to JSON and put in the request body just fine. 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 greatly appreciated!

3
Did you ever find a solution to this? I can replicate your issue exactly. - Aran Mulholland
This is sort of semantics but if you're going to send an authorization token with a request, it may as well be in the Authorization header. - barndog

3 Answers

1
votes

There's a Gist at https://gist.github.com/onelittlefish/5970616 that provides a nice extension to RKObjectManager that will allow you to add query params to a PUT or POST request.

Just drop those files into your project, import the header, and you can use something similar to the answer by @giuseppe (which adds the params to the body, not the path). The only difference is changing 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!");

              }
];
0
votes

In my implementation I have added query 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
votes

As simple as reading the many tutorials available on the web. However:

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);

                  }
 ];