2014-02-13 97 views
0

所以我試圖使用[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。

+0

您需要提供一個對象(與關聯的路由)或路徑。顯示一些代碼。使用Charles查看它發送的內容。說明服務器的期望。你設置了什麼序列化類型? – Wain

+0

我在上面添加了一些代碼,希望有所幫助。 – sfeuerstein

+0

您可以配置restkit網絡日誌記錄以獲取更多詳細信息:'RKLogConfigureByName(「RestKit/Network *」,RKLogLevelTrace);' – vokilam

回答

1

最後弄明白了,當然這是一個非常愚蠢的問題。服務器期待一個params字典,但我的對象管理器的requestSerializationMIMEType被設置爲RKMIMETypeJSON。所以,一旦我評論說,請求行爲良好,對象爲零,參數設置爲字典@{@"email": email, @"password": password}