I am new to both IOS and RestKit and can not figure out how to do the following:
1. a simple POST to a webservice
A) with an object payload
B) Have RestKit Auto Serialize the obj c object into Json
2. Receive the Response
A) Parse response and extract only certain fields and load into obj c object
My setup is as follows:
Json response from webservice:
{
"user": {
"id":0,
"email":"String",
},
"errorCode": 0
}
Response Object from Webservice in obj c:
@interface CreateAccountResponse : NSObject
@property (nonatomic, copy) NSString *id;
@property (nonatomic, copy) NSString *email;
@property (nonatomic) NSInteger *errorCode;
@end
Request Object in obj C:
@interface CreateAccountRequestModel : NSObject
@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, copy) NSString *first_name;
@property (nonatomic, copy) NSString *last_name;
@end
Webservice expects this Json in POST Request:
{
"email":"String",
"password":"String",
"first_name":"String",
"last_name":"String"
}
URL is: www.hostname.com/api/AccountManager/createAccount
and it only accepts POST requests.
What I have tried:
-(void)SendCreateAccountRequest:(CreateAccountRequestModel*)createActModel
{
NSString *createAccountUrl = @"www.hostname.com/api/AccountManager/createAccount";
NSLog(@"SendCreateAccountRequest");
RKObjectMapping* articleMapping = [RKObjectMapping mappingForClass: [CreateAccountResponse class]];
[articleMapping addAttributeMappingsFromDictionary:@{
@"id": @"user.id",
@"email": @"user.email",
@"errorCode": @"errorCode"
}];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:articleMapping method:RKRequestMethodAny pathPattern:nil keyPath:@"createAccount" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSURL *URL = [NSURL URLWithString:createAccountUrl];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
RKObjectRequestOperation *objectRequestOperation = [[RKObjectRequestOperation alloc] initWithRequest:request responseDescriptors:@[ responseDescriptor ]];
[objectRequestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult)
{
RKLogInfo(@"Successful Response: %@", mappingResult.array);
}
failure:^(RKObjectRequestOperation *operation, NSError *error)
{
RKLogError(@"Operation failed with error: %@", error);
}];
[objectRequestOperation start];
I know I am not correctly mapping the response from the webservice to an object and I am not correctly creating the POST request. Like I said I am new, so the more details you give the better :)
User Object
POST http://www.hostname.com/api/AccountManager/createAccount
response JSON
Create On Server
PS: You'd better to read Restkit's README completely!!
PS2: error code by HTTP statusCode is better