So I'm trying to use [RKObjectManager postObject:path:parameters:success:failure:]
but am having some trouble getting it working with my login POST request. For some reason I keep getting a response back from my server saying that the parameters for email and password are required, even though I'm passing the following dictionary in for parameters:
NSDictionary *params = @{@"email": @"fakeEmail@fakeDomain.com, @"password": @"test123!"};
When I log out the RKObjectRequestOperation
it doesn't show any parameters on the request. Do I have to pass an object in with the request? If so, what object would I pass in?
(Previously I was just using and AFJSONRequestOperation
, but I would like to update the app to use RestKit and take advantage of the easy object mapping it provides).
Any help would be appreciated.
EDIT With More Code:
I have a subclass of RKObjectManager called UserAuthService, using RKMIMETYPEJSON as the requestSerializationMIMEType, with the following request descriptor setup:
// User
RKResponseDescriptor *userResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[UserAuthMappingProvider userMapping]
method:RKRequestMethodPOST
pathPattern:@"user/login"
keyPath:@"response.users"
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[self addResponseDescriptor:userResponseDescriptor];
The method I'm using to actually request is:
- (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);
}
}];
}
the userMapping method in UserAuthMappingProvider looks like this:
+ (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;
}
and the UserObject (with each set to @dynamic in the .m):
@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
The error I'm getting back is:
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'"}
Basically my goal is to take the success response of the user/login call and map it to the UserObject.
Finally figure it out, and of course it was a really stupid issue. The server was expecting a dictionary of params, but my object manager's requestSerializationMIMEType
was set to RKMIMETypeJSON
. So, once I commented that line out the request worked fine with the object being nil and the parameters being set to a dictionary of @{@"email": email, @"password": password}