我正在尝试使用 RestKit 0.20.0 登录到运行 Devise 的 RoR 后端。我正在尝试使用以下参数实现请求:
Parameters: {"password"=>"[FILTERED]", "email"=>"email@mail.com"}
但在服务器上实际收到的是以下内容:
Parameters: {"password"=>"[FILTERED]", "email"=>"email@mail.com", "session"=>{"password"=>"[FILTERED]", "email"=>"email@mail.com"}}
这个会话参数来自哪里,我不知道。执行 GET 时不添加,仅在执行 POST 时添加。登录通过,但响应完全错误。通过使用正确的参数列表,可以给出正确的响应。下面是发送请求的代码。关于这个会话参数来自哪里的任何想法?
- (void)loginWithblock:(void (^)(bool))block
{
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:_username, @"email", _password, @"password", nil];
NSString *url = [[MBMSettingsHelper sharedSettingsHelper] authenticationURL];
[[RKObjectManager sharedManager] postObject:nil path:url parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
block(YES);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
block(NO);
}];
}
在我的 appdelegate 中,我有以下映射器:
// Mappers for authentication
RKObjectMapping *requestMappingForAuthenticate = [RKObjectMapping requestMapping];
[requestMappingForAuthenticate addAttributeMappingsFromDictionary:@{
@"username" : @"email",
@"password": @"password"
}];
RKRequestDescriptor *requestDescriptorForAuthenticate = [RKRequestDescriptor requestDescriptorWithMapping:requestMappingForAuthenticate objectClass:[MBMUserLoginRequest class] rootKeyPath:nil];
RKObjectMapping *responseMappingForAuthenticate = [RKObjectMapping requestMapping];
[responseMappingForAuthenticate addAttributeMappingsFromDictionary:@{
@"success": @"success",
@"auth_token": @"token",
@"email": @"username",
@"id": @"userId"}];
RKResponseDescriptor *responseDescriptorForAuthenticate = [RKResponseDescriptor responseDescriptorWithMapping:responseMappingForAuthenticate pathPattern:nil keyPath:nil statusCodes:nil];
[objectManager addResponseDescriptor:responseDescriptorForAuthenticate];
[objectManager addRequestDescriptor:requestDescriptorForAuthenticate];
编辑:
RestKit 日志发布在下面。看起来登录后,映射器开始使用完全不同的模型(在这种情况下为船)映射登录响应?
2013-05-24 01:33:59.649 xxx[8153:637f] D restkit.object_mapping:RKMapperOperation.m:241 Asked to map source object {
"auth_token" = xxx;
email = "xxx";
id = 2;
success = 1;
} with mapping <RKObjectMapping:0x1d5213a0 objectClass=NSMutableDictionary propertyMappings=(
"<RKAttributeMapping: 0x1d51a120 id => boatId>",
"<RKAttributeMapping: 0x1d54ec90 year => year>",
"<RKAttributeMapping: 0x1d54ecb0 factory => manufacturer>",
"<RKAttributeMapping: 0x1d528b40 gearnumber => sterndriveNumber>",
"<RKAttributeMapping: 0x1d528b80 harbour => homeHarbour>",
"<RKAttributeMapping: 0x1d54ea80 enginemanufactory => engineManufacturer>",
"<RKAttributeMapping: 0x1d54e9f0 updated_at => updatedAt>",
"<RKAttributeMapping: 0x1d54ed00 created_at => createdAt>",
"<RKAttributeMapping: 0x1d54ee70 docknumber => dockNumber>",
"<RKAttributeMapping: 0x1d54ed30 insurancecompany => insuranceCompany>",
"<RKAttributeMapping: 0x1d54edd0 model => model>",
"<RKAttributeMapping: 0x1d54ede0 name => name>",
"<RKAttributeMapping: 0x1d54f0e0 enginemodel => engineModel>",
"<RKAttributeMapping: 0x1d54f170 enginenumber => engineNumber>",
"<RKAttributeMapping: 0x1d54ed40 engineyear => engineYear>",
"<RKAttributeMapping: 0x1d54ed50 geartype => sterndriveType>"
)>
2013-05-24 01:33:59.652 xxx[8153:637f] D restkit.object_mapping:RKMappingOperation.m:862 Starting mapping operation...
2013-05-24 01:33:59.653 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:863 Performing mapping operation: <RKMappingOperation 0x1e325070> for '__NSDictionaryM' object. Mapping values from object {
"auth_token" = xxx;
email = "xxx";
id = 2;
success = 1;
} to object {
} with object mapping (null)
2013-05-24 01:33:59.655 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:458 Mapping attribute value keyPath 'id' to 'boatId'
2013-05-24 01:33:59.656 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:480 Mapped attribute value from keyPath 'id' to 'boatId'. Value: 2
2013-05-24 01:33:59.657 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'year'
2013-05-24 01:33:59.658 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'factory'
2013-05-24 01:33:59.659 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'gearnumber'
2013-05-24 01:33:59.659 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'harbour'
2013-05-24 01:33:59.660 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'enginemanufactory'
2013-05-24 01:33:59.661 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'updated_at'
2013-05-24 01:33:59.662 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'created_at'
2013-05-24 01:33:59.663 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'docknumber'
2013-05-24 01:33:59.664 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'insurancecompany'
2013-05-24 01:33:59.664 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'model'
2013-05-24 01:33:59.665 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'name'
2013-05-24 01:33:59.666 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'enginemodel'
2013-05-24 01:33:59.667 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'enginenumber'
2013-05-24 01:33:59.667 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'engineyear'
2013-05-24 01:33:59.668 xxx[8153:637f] T restkit.object_mapping:RKMappingOperation.m:525 Did not find mappable attribute value keyPath 'geartype'
2013-05-24 01:33:59.669 xxx[8153:637f] D restkit.object_mapping:RKMappingOperation.m:926 Finished mapping operation successfully...
2013-05-24 01:33:59.670 xxx[8153:637f] D restkit.object_mapping:RKMapperOperation.m:414 Finished performing object mapping. Results: {
"<null>" = {
boatId = 2;
};
编辑 2:当我删除我的其他 RKResponseDescriptors 时,一切正常。所以问题是在映射发生时以某种方式选择了错误的 RKResponseDescriptor ...