带有查询参数的 RestKit postObject
Posted
技术标签:
【中文标题】带有查询参数的 RestKit postObject【英文标题】:RestKit postObject with Query Parameters 【发布时间】:2013-01-11 08:05:24 【问题描述】:我正在使用 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=="
非常感谢任何帮助!
【问题讨论】:
您找到解决方案了吗?我可以准确地复制您的问题。 这是一种语义,但如果您要通过请求发送授权令牌,它也可能在 Authorization 标头中。 【参考方案1】:https://gist.github.com/onelittlefish/5970616 有一个 Gist,它为 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!");
];
【讨论】:
【参考方案2】:在我的实现中,我在 URL 本身中添加了查询参数:
RKManagedObjectRequestOperation *operation = [RKObjectManager.sharedManager appropriateObjectRequestOperationWithObject:self method:RKRequestMethodPOST path:[NSString stringWithFormat:@"/api/v1/users/3/locations?token=%@",token] parameters:nil];
【讨论】:
您必须使用该路径添加响应描述符,以便对象管理器正确执行对象映射。【参考方案3】:就像阅读网络上的许多教程一样简单。 然而:
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);
];
【讨论】:
以上是关于带有查询参数的 RestKit postObject的主要内容,如果未能解决你的问题,请参考以下文章
restkit 映射嵌套数组为空(带有 json 响应的嵌套 restkit 请求)