带有查询参数的RestKit postObject

时间:2013-01-11 08:05:24

标签: ios restkit

我正在使用RestKit而我正在尝试使用查询参数(token=<token>形式的身份验证令牌)发布一个对象,但我无法弄清楚如何让它工作。这就是我正在做的......

首先,我将请求对象映射添加到管理器:

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

然后我提出请求:

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

发出请求时,Location对象被序列化为JSON并将其放入请求体中。但是,不是将令牌添加到查询字符串中,而是将其作为JSON添加到请求正文中。

示例:

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

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

https://gist.github.com/onelittlefish/5970616有一个要点,它为RKObjectManager提供了一个很好的扩展,允许你将查询参数添加到PUT或POST请求。

只需将这些文件放入项目中,导入标题,然后就可以使用类似于@giuseppe的答案(将参数添加到正文,而不是路径)。唯一的区别是将parameters更改为queryParameters - 您的电话可能如下所示:

[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 :(得分:0)

在我的实现中,我在URL本身中添加了查询参数:

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

答案 2 :(得分:0)

就像阅读网上提供的许多教程一样简单。 但是:

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

                  }
 ];