Matt
Matt

Reputation: 674

Unable to POST to Server using RestKit

I have been trying this for quite sometime now, but I just couldn't be able to post successfully to my server. I have read this, github object mapping overview for the Restkit and, the Gist restKit tutorial.

My Problem

I am trying to post sign in information(nickname,hardwareId,phone model) and get the AccountId from my server. But I am getting this response from the console:

GuessTheImage[6832:70b] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<AccountsClass 0x8c5cba0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key AccountInfo.'

What I think is the problem

I believe I am messing up the way I store the data right before the mapping part. But I am not sure where exactly.

The JSON that I am trying post should look like this

{
  "DeviceType": "sample string 1",
  "HardwareId": "sample string 2",
  "NickName": "sample string 3",
  "AccountId": 4
}

Overview of the program I created the variables for mapping in the AccountClass. From loginviewcontroller, I get the device type, hardwareId and nickname after the user clicks login button and assign those variables to the variables in AccountClass and map it POST if afterwards. AccountClass.h

#import <Foundation/Foundation.h>

@interface AccountsClass : NSObject

@property (nonatomic, strong,retain)NSString *DeviceType;
@property (nonatomic,strong)NSNumber *AccountId;
@property (nonatomic,strong) NSString *NickName;
@property (nonatomic,strong) NSNumber *HardwareId;

@end

LoginViewController.h

#import <UIKit/UIKit.h>

@interface LoginViewController : UIViewController<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UITextField *usernameTextField;
@property (strong, nonatomic) IBOutlet UIButton *submitButton;
@property (nonatomic,readonly) NSUUID *identifierForVendor;
@property(nonatomic, readonly, retain) NSString *model;
@property (nonatomic,readonly,retain)NSString *StoreIdentifierForVendor;
@property (nonatomic,readonly,retain)NSString *StoreTheModel;
- (IBAction)submit:(id)sender;
@property (nonatomic,strong)NSString *nickname;
@end

LoginViewController.m

#import "LoginViewController.h"
#import <RestKit/RestKit.h>
#import "AccountsClass.h"
@interface LoginViewController ()

@end

@implementation LoginViewController
@synthesize usernameTextField;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmissKeyboard)];
    [self.view addGestureRecognizer:tap];
    [usernameTextField setDelegate:self];


}
-(void)loadPostRequest
{
    _StoreIdentifierForVendor = [[[UIDevice currentDevice]identifierForVendor]UUIDString];
    _StoreTheModel = [UIDevice currentDevice].model;
    _nickname = usernameTextField.text;
     AccountsClass *AccountInfo = [[AccountsClass alloc] init];
    AccountInfo.NickName = _nickname;
    NSNumberFormatter *hardwareIdentification = [[NSNumberFormatter alloc]init];
    [hardwareIdentification setNumberStyle:NSNumberFormatterScientificStyle];
    NSNumber *hwreID =[hardwareIdentification numberFromString:_StoreIdentifierForVendor];
    AccountInfo.HardwareId = hwreID;
    AccountInfo.DeviceType =_StoreTheModel;
      RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[AccountsClass class]];
   [responseMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];

    NSIndexSet *statusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); // Anything in 2xx
    RKResponseDescriptor *AccountDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:statusCodes];


    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping]; // objectClass == NSMutableDictionary
    [requestMapping addAttributeMappingsFromArray:@[@"AccountInfo.NickName", @"AccountInfo.HardwareId", @"AccountInfo.DeviceType",@"AccountInfo.AccountId"]];
    // For any object of class Article, serialize into an NSMutableDictionary using the given mapping and nest
    // under the 'article' key path
    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping objectClass:[AccountInfo class] rootKeyPath:nil method:RKRequestMethodAny];
    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"https://picquiz.azurewebsites.net"]];
                                [manager addRequestDescriptor:requestDescriptor];
                                [manager addResponseDescriptor:AccountDescriptor];
                                // POST to create
                                [manager postObject:AccountInfo path:@"/Accounts" parameters:nil success:nil failure:nil];

}
-(void)dissmissKeyboard
{
    [usernameTextField resignFirstResponder];
}

- (IBAction)submit:(id)sender {
    [self loadPostRequest];
}
@end

I will appreciate your help.

Upvotes: 0

Views: 101

Answers (1)

Wain
Wain

Reputation: 119031

Your problem appears to be:

[requestMapping addAttributeMappingsFromArray:@[@"AccountInfo.NickName", @"AccountInfo.HardwareId", @"AccountInfo.DeviceType",@"AccountInfo.AccountId"]];

because the data that you are trying to POST doesn't have nested data like that. It looks like it should be:

[requestMapping addAttributeMappingsFromArray:@[@"NickName", @"HardwareId", @"DeviceType",@"AccountId"]];

Upvotes: 1

Related Questions