无法将RestKit 0.2提供给POST参数

时间:2014-02-13 18:32:29

标签: ios iphone objective-c restkit rkobjectmapping

所以我正在尝试使用[RKObjectManager postObject:path:parameters:success:failure:]但是在使用我的登录POST请求时遇到了一些麻烦。出于某种原因,我不断收到服务器的回复,说电子邮件和密码的参数是必需的,即使我将以下字典传递给参数:

NSDictionary *params = @{@"email": @"fakeEmail@fakeDomain.com, @"password": @"test123!"};

当我退出RKObjectRequestOperation时,它不会在请求中显示任何参数。我是否必须通过请求传递对象?如果是这样,我会传递什么对象?

(之前我只是使用和AFJSONRequestOperation,但我想更新应用程序以使用RestKit并利用它提供的简单对象映射。

任何帮助都将不胜感激。

使用更多代码编辑:

我有一个名为UserAuthService的RKObjectManager子类,使用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。

1 个答案:

答案 0 :(得分:1)

最后弄清楚了,当然这是一个非常愚蠢的问题。服务器期待一个params字典,但我的对象管理器requestSerializationMIMEType设置为RKMIMETypeJSON。所以,一旦我评论该行,请求工作正常,对象为nil,参数设置为@{@"email": email, @"password": password}的字典