4

我正在使用 RestKit,我正在尝试发布一个带有查询参数的对象(一个 auth 形式的令牌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=="}

任何帮助是极大的赞赏!

4

3 回答 3

1

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

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

[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!");

              }
];
于 2015-04-04T18:04:05.867 回答
0

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

RKManagedObjectRequestOperation *operation = [RKObjectManager.sharedManager  appropriateObjectRequestOperationWithObject:self method:RKRequestMethodPOST path:[NSString stringWithFormat:@"/api/v1/users/3/locations?token=%@",token] parameters:nil];
于 2013-01-12T13:00:25.207 回答
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);

                  }
 ];
于 2013-12-08T09:08:51.317 回答