I am using RestKit to make a POST request. I am able to make simple POST requests successfully. This includes the parameters with simple NSStrings. Unfortunately I cannot properly make POST requests when the parameters require OBJECTS.
For an example, here is what my NSDictionary of parameters looks like:
NSDictionary *sectionStepParams = [NSDictionary dictionaryWithObjectsAndKeys:sectionCodeString,@"1",lastSectionStepCode,@"2",_currentState,@"State",nil];
Notice that the key for "State" is an State object. Currently this is my State.h class:
@interface State : NSObject
@property (nonatomic,strong) NSMutableArray *SectionStepResponses;
@property (nonatomic,copy) NSString *Token;
+(RKObjectMapping*)mapping;
-(void)output;
@end
It's a very simple header. Just a NSString and an Array of SectionStep objects (another simple class).
How do I pass the state object properly? Putting in the State object into the NSDictionary does not give me a proper JSON response back. Do I have to convert this state object into data that can be recognized for the parameter NSDictionary?
Here is my full code for making a POST request:
RKResponseDescriptor *stateRD = [RKResponseDescriptor responseDescriptorWithMapping:[State mapping] method:RKRequestMethodPOST pathPattern:nil keyPath:@"Response.State" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
NSDictionary *sectionStepParams = [NSDictionary dictionaryWithObjectsAndKeys:sectionCodeString,@"1",lastSectionStepCode,@"2",_currentState,@"State",nil];
//Create Object Manager
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:baseurl];
[objectManager addResponseDescriptorsFromArray:@[stateRD]];
//Create Request Operation
RKObjectRequestOperation *requestOperation = [objectManager appropriateObjectRequestOperationWithObject:nil method:RKRequestMethodPOST path:@"getnextstep" parameters:sectionStepParams];
//Set Request Operation Success/Fail block
[requestOperation setCompletionBlockWithSuccess:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"success %@",mappingResult.array);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"failed");
}];
Note: This service requires a state object and passes back a state object. I am able to get back a state object fine (it's JSON is empty), the problem is just giving the service a proper state object.
Thank You!