I'm trying to login to an application in Objective-C using RestKit but when I type the email and password it has the following error in the console:
AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7ffbd0f5f130> { URL: http://localhost:3000/users/sign_in.json } { status code: 401, headers {
"Cache-Control" = "no-cache";
Connection = "Keep-Alive";
"Content-Length" = 61;
"Content-Type" = "application/json; charset=utf-8";
Date = "Wed, 25 Feb 2015 21:18:57 GMT";
Server = "WEBrick/1.3.1 (Ruby/2.0.0/2014-05-08)";
"X-Content-Type-Options" = nosniff;
"X-Frame-Options" = SAMEORIGIN;
"X-Request-Id" = "c8c3c7c6-db3b-432d-82bb-c14b882f42a9";
"X-Runtime" = "0.011115";
"X-Ua-Compatible" = "chrome=1";
"X-Xss-Protection" = "1; mode=block";
} }, NSLocalizedDescription=Expected status code in (200-299), got 401}
I think it's not my api because I checked it and everything is fine, more likely to be obj-c because I'm new to this language.
THIS IS MY WEBSERVICE
#import "JVWebService.h"
#import <RestKit/RestKit.h>
#import "AppDelegate.h"
#import "JVUtils.h"
static NSString *kServerURL = @"http://localhost:3000";
@interface JVWebService ()
@property (strong, nonatomic) RKObjectManager *restKitObjectManager;
@property (strong, nonatomic) NSDictionary *adAttributes;
@property (strong, nonatomic) NSDictionary *postAdAttributes;
@property (strong, nonatomic) NSDictionary *userAttributes;
@property (strong, nonatomic) NSDictionary *postUserAttributes;
@end
#define kSuccessStatusCode RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)
@implementation JVWebService
+ (instancetype)sharedService {
static JVWebService *sharedService = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedService = [[self alloc] init];
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
sharedService.restKitObjectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:kServerURL]];
[sharedService.restKitObjectManager.HTTPClient setAuthorizationHeaderWithUsername:[[[AppDelegate sharedDelegate] currentUser] email]
password:[[[AppDelegate sharedDelegate] currentUser] password]];
});
return sharedService;
}
#pragma mark - User
- (void)getUserForEmail:(NSString *)email andPassword:(NSString *)password {
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:User.class];
[objectMapping addAttributeMappingsFromDictionary:self.userAttributes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:self.postUserAttributes];
NSString *path = @"/users/sign_in.json";
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:User.class
rootKeyPath:@"user"
method:RKRequestMethodAny];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"user"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addRequestDescriptor:requestDescriptor];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
User *user = [User new];
user.email = email;
user.password = password;
[[NSUserDefaults standardUserDefaults] setObject:[[NSUUID UUID] UUIDString] forKey:@"authencity_token"];
NSDictionary *params = @{@"authenticity_token" : [[NSUserDefaults standardUserDefaults] objectForKey:@"authencity_token"]};
[self.restKitObjectManager.HTTPClient setAuthorizationHeaderWithUsername:email password:password];
[self.restKitObjectManager postObject:user path:path parameters:params success:^(RKObjectRequestOperation *operation,
RKMappingResult *result){
User *user = (User *)result.array.firstObject;
user.password = password;
[[AppDelegate sharedDelegate] login:user];
[[AppDelegate sharedDelegate] setLoggedViaFacebook:NO];
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:user];
} failure:^(RKObjectRequestOperation *operation, NSError *error){
RKLogError(@"Operation failed with error: %@", error);
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
/*
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:User.class];
[objectMapping addAttributeMappingsFromDictionary:self.userAttributes];
NSString *path = @"/login.json";
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"user"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
[self.restKitObjectManager.HTTPClient setAuthorizationHeaderWithUsername:email password:password];
[self.restKitObjectManager getObjectsAtPath:path parameters:nil success:^(RKObjectRequestOperation *operation,
RKMappingResult *result){
User *user = (User *)result.array.firstObject;
user.password = password;
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:user];
} failure:^(RKObjectRequestOperation *operation, NSError *error){
RKLogError(@"Operation failed with error: %@", error);
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
*/
}
- (void)createUser:(User *)user withProfileImage:(UIImage *)profileImage {
NSString *path = @"/users.json";
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:User.class];
[objectMapping addAttributeMappingsFromDictionary:self.userAttributes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:self.postUserAttributes];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:User.class
rootKeyPath:@"user"
method:RKRequestMethodAny];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"user"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addRequestDescriptor:requestDescriptor];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
NSMutableURLRequest *request = [self.restKitObjectManager multipartFormRequestWithObject:user method:RKRequestMethodPOST path:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:UIImagePNGRepresentation(profileImage)
name:@"user[avatar]"
fileName:@"profileImage.jpg"
mimeType:@"image/jpg"];
}];
RKObjectRequestOperation *operation = [self.restKitObjectManager objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
User *user = (User *)result.array.firstObject;
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:user];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager enqueueObjectRequestOperation:operation];
[self.restKitObjectManager removeRequestDescriptor:requestDescriptor];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
}
- (void)updateUser:(User *)user withProfileImage:(UIImage *)profileImage {
NSString *path = @"/users.json";
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:User.class];
[objectMapping addAttributeMappingsFromDictionary:self.userAttributes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:self.postUserAttributes];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:User.class
rootKeyPath:@"user"
method:RKRequestMethodAny];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"user"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addRequestDescriptor:requestDescriptor];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
NSDictionary *params = @{@"authencity_token" : [[NSUserDefaults standardUserDefaults] objectForKey:@"authencity_token"]};
NSMutableURLRequest *request = [self.restKitObjectManager multipartFormRequestWithObject:user method:RKRequestMethodPATCH path:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// [formData appendPartWithFileData:UIImagePNGRepresentation(profileImage)
// name:@"user[avatar]"
// fileName:@"profileImage.jpg"
// mimeType:@"image/jpg"];
}];
RKObjectRequestOperation *operation = [self.restKitObjectManager objectRequestOperationWithRequest:request success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
[[AppDelegate sharedDelegate] login:(User *) result.array.firstObject];
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:result.firstObject];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager enqueueObjectRequestOperation:operation];
[self.restKitObjectManager removeRequestDescriptor:requestDescriptor];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
}
- (void)logout {
NSString *path = @"/users/sign_out.json";
RKObjectMapping *objectMapping = [RKObjectMapping mappingForClass:User.class];
[objectMapping addAttributeMappingsFromDictionary:self.userAttributes];
RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
[requestMapping addAttributeMappingsFromDictionary:self.postUserAttributes];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
objectClass:User.class
rootKeyPath:@"user"
method:RKRequestMethodAny];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:objectMapping
method:RKRequestMethodAny
pathPattern:path
keyPath:@"user"
statusCodes:kSuccessStatusCode];
[self.restKitObjectManager addRequestDescriptor:requestDescriptor];
[self.restKitObjectManager addResponseDescriptor:responseDescriptor];
NSDictionary *params = @{@"authenticity_token" : [[NSUserDefaults standardUserDefaults] objectForKey:@"authencity_token"]};
[self.restKitObjectManager deleteObject:[[AppDelegate sharedDelegate] currentUser] path:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
[[AppDelegate sharedDelegate] logout];
if ([[AppDelegate sharedDelegate] isLoggedViaFacebook]) [[AppDelegate sharedDelegate] closeFacebookSession];
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:nil];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
[self.restKitObjectManager removeRequestDescriptor:requestDescriptor];
[self.restKitObjectManager removeResponseDescriptor:responseDescriptor];
}
- (void)sendResetPasswordInstructionsToEmail:(NSString *)email {
NSString *path = @"/send_lost_password.json";
NSDictionary *params = @{@"email" : email};
[self.restKitObjectManager getObject:nil path:path parameters:params success:^(RKObjectRequestOperation *operation, RKMappingResult *result) {
if ([self.serviceDelegate respondsToSelector:@selector(successfulRequestDidReturnObject:)])
[self.serviceDelegate successfulRequestDidReturnObject:nil];
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
RKLogError(@"Operation failed with error: %@", error);
if ([self.serviceDelegate respondsToSelector:@selector(requestDidFailWithError:)])
[self.serviceDelegate requestDidFailWithError:error];
}];
}
@end