所以我試圖使用[RKObjectManager postObject:path:parameters:success:failure:]
,但在使用我的登錄POST請求時遇到了一些麻煩。出於某種原因,我一直從我的服務器說,所需要的電子郵件和密碼參數得到響應回來,即使我通過下面的字典中的參數:無法獲得RestKit 0.2到POST參數
NSDictionary *params = @{@"email": @"[email protected], @"password": @"test123!"};
當我註銷RKObjectRequestOperation
它沒有在請求中顯示任何參數。我是否必須在請求中傳遞一個對象?如果是這樣,我會傳入什麼對象? (之前我只是使用和AFJSONRequestOperation
,但我想更新應用程序以使用RestKit並利用它提供的簡單對象映射)。
任何幫助,將不勝感激。
編輯與更多的代碼:
我有RKObjectManager的子類,稱爲UserAuthService,使用RKMIMETYPEJSON作爲requestSerializationMIMEType,與下面的請求描述符設置:
// User
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[UserAuthMappingProvider userMapping]
method:RKRequestMethodPOST
pathPattern:@"user/login"
keyPath:@"response.users"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:userResponseDescriptor];
的方法我用實際要求是:
- (void)logUserInWithEmail:(NSString *)email andPassword:(NSString *)password success:(void (^)(UserObject *))success failure:(void (^)(RKObjectRequestOperation *, NSError *))failure
{
// Request Params
NSDictionary *params = @{@"email": email, @"password": password};
NSLog(@"Params: %@", params);
[self postObject:nil path:@"user/login" parameters:params
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult){
if (success)
{
NSArray *userArray = [mappingResult array];
success([userArray firstObject]);
}
}
failure:^(RKObjectRequestOperation *operation, NSError *error){
NSLog(@"Error: %@", error);
if (failure)
{
failure(operation, error);
}
}];
}
在UserAuthMappingProvider的userMapping方法看起來像這樣:
+ (RKEntityMapping *)userMapping
{
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
RKEntityMapping *userMapping = [RKEntityMapping mappingForEntityForName:@"User" inManagedObjectStore:appDelegate.managedObjectStore];
userMapping.identificationAttributes = @[ @"uuid" ];
[userMapping addAttributeMappingsFromDictionary:@{@"email": @"email",
@"first_name": @"firstName",
@"last_name": @"lastName",
@"is_logged_in": @"isLoggedIn",
@"site_id": @"siteID",
@"user_name": @"username",
@"uuid": @"uuid"}];
return userMapping;
}
和UserObject(每一組中.M到@dynamic):
@interface UserObject : NSManagedObject
@property (strong, nonatomic) NSString *email;
@property (strong, nonatomic) NSString *firstName;
@property (strong, nonatomic) NSString *lastName;
@property (assign, nonatomic) BOOL isLoggedIn;
@property (strong, nonatomic) NSNumber *siteID;
@property (strong, nonatomic) NSString *username;
@property (strong, nonatomic) NSString *uuid;
@end
錯誤我收到的回覆是:
Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (200-299), got 400" UserInfo=0x8eadbf0 {NSLocalizedRecoverySuggestion={"required_parameters":{"email":"string","password":"string"},"status":"failed","message":"Insufficient information passed. see 'required_parameters'"}
基本上我的目標是獲取用戶/登錄調用的成功響應並將其映射到UserObject。
您需要提供一個對象(與關聯的路由)或路徑。顯示一些代碼。使用Charles查看它發送的內容。說明服務器的期望。你設置了什麼序列化類型? – Wain
我在上面添加了一些代碼,希望有所幫助。 – sfeuerstein
您可以配置restkit網絡日誌記錄以獲取更多詳細信息:'RKLogConfigureByName(「RestKit/Network *」,RKLogLevelTrace);' – vokilam