0
votes

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!

1

1 Answers

0
votes

Whether you convert your State object into a dictionary is personal choice, both will work. What's missing is a mapping to tell RestKit what it should do when it finds a 'non-standard' object. Strings, dates and arrays can be handled automatically but for dictionaries and custom objects RestKit expects to be provided with a list of keys that should be mapped. This is provided in exactly the same way as you would when receiving the JSON (instead of sending it).


Your code has a few issues:

  1. You need a request descriptor to tell RestKit how to serialise the outgoing data (usually this can be the inverseMapping of your response descriptor.
  2. You need to post the object, not add the object as a parameter (currently you set the object to be posted to nil). This is assuming that the original object should be updated by the response...
  3. You should set the path pattern of the response descriptor so RestKit knows that it is the one to be used.