我正在使用 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=="}
任何帮助是极大的赞赏!