7

I would like to know if there is a specific tutorial on how to do a POST request with RESTKit. I have looked at some tutorials but I haven't found any that say, "This is exactly how you do a POST request with RESTKit." Help is much appreciated.

comrod
  • 343
  • 7
  • 17

1 Answers1

11

Assuming you already have a mapped model, you can simply perform this:

First, set a requestDescriptor with the inverseMapping of your responseDescriptor, assuming you have one with your mapping.

//This is used for mapping responses, you already should have one of this. PS:[Data mapping] is a method that returns an RKObjectMapping for my model. You should create yours or use a previous created one
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Data mapping] pathPattern:nil keyPath:@"data" statusCodes:statusCodeSet];
[[RKObjectManager sharedInstance] addResponseDescriptor:responseDescriptor];

//Inverse mapping, to perform a POST
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[Data mapping] inverseMapping]  objectClass:[Data class] rootKeyPath:nil];
[[RKObjectManager sharedInstance] addRequestDescriptor:requestDescriptor];

After that, to perform a POST, just simply call the method below. Restkit will get the instance that you are trying to post, serialize it and send to the path chosen.

[[RKObjectManager sharedInstance] postObject:instanceOfYourModel path:yourPathHere parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Success");

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error");
}];

If you don't have a mapped model, let me know so we can try something else.

Lucas Eduardo
  • 11,525
  • 5
  • 44
  • 49
  • I don't really know what a mapped model is. Is it this: RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Login class]]; [mapping addAttributeMappingsFromArray:@[@"username", @"password"]]; return mapping; – comrod Jul 31 '13 at 03:10
  • 1
    Yes. Your object mapping has a method [mapping inverseMapping]. Just create a request descriptor, like I shown, andd it to your RKObject Manager, and you will be able to perform a postObject. Any doubts, please ask – Lucas Eduardo Jul 31 '13 at 10:45
  • 1
    Lucas, I wish I could accept the answer: it helped me a lot. +1 it. – PJC Sep 16 '13 at 17:08
  • The value of "parameters" argument must be passed with a dictionary object to avoid request.body = null in method [[RKObjectManager sharedInstance] postObject:instanceOfYourModel path:yourPathHere parameters:nil success:.... That is not only for the POST method but the GET as well. – Thomas.Benz Oct 22 '13 at 20:29
  • 1
    At least when I last tested Restkit (version 0.20.0), the `postObject:` method automatically serializes the object passed in the parameter, using the `RKRequestDescriptor`, and send to server in the body of the request, without the need of passing any `parameters`, like you would have to if was using `AFNetwork`, for example. – Lucas Eduardo Oct 22 '13 at 20:39
  • I don't have mapping , could you please provide me this methodology ? – wod Nov 28 '13 at 11:02
  • Hello @wod, above you can see, in Restkit documentation, how to create a mapping for your model, using the class `RKObjectMapping`. This instance of `RKObjectMapping` is the mapping object that will be used in the code of this answer. https://github.com/RestKit/RestKit/wiki/Object-mapping – Lucas Eduardo Nov 28 '13 at 11:49
  • I need to used postObject without entity mapping – wod Nov 28 '13 at 12:27
  • 1
    Well, unfortunately that's not possible. If you really don't want to use a entity mapping, you can use `postPath:parameters:success:failure:` method of your `[RKObjectManager sharedManager].HTTPClient`. See the documentation of this method for more information: http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFHTTPClient.html#//api/name/postPath:parameters:success:failure: – Lucas Eduardo Nov 28 '13 at 14:29
  • When i used this methodology i get null body ,could you help me please? – wod Dec 16 '13 at 14:32
  • It's almost impossible to tell the problem without more informations. You can open a question with details and post the link here, I'd be glad to help. – Lucas Eduardo Dec 16 '13 at 22:11
  • Hi @Lucas Eduardo I am not able to post on this url, i am getting 500 error and "Server was unable to process request. ---> Root element is missing." error message on below link. please help me http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit – kulss Jun 14 '15 at 11:26
  • My working code is :- (void)postOnServer{ NSURL *baseURL = [NSURL URLWithString:@"http://www.w3schools.com"]; AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; RKObjectMapping *venueMapping = [RKObjectMapping mappingForClass:[Venue class]]; [venueMapping addAttributeMappingsFromArray:@[@"Celsius"]]; RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:venueMapping pathPattern:@"/webservices/tempconvert.asmx/CelsiusToFahrenheit" – kulss Jun 14 '15 at 11:31
  • keyPath:@"string" statusCodes:[NSIndexSet indexSetWithIndex:200]]; [objectManager addResponseDescriptor:responseDescriptor]; RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[venueMapping inverseMapping] objectClass:[Venue class] rootKeyPath:nil]; – kulss Jun 14 '15 at 11:33
  • success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { NSLog(@"Success"); } failure:^(RKObjectRequestOperation *operation, NSError *error) { NSLog(@"Error"); }]; } – kulss Jun 14 '15 at 11:34
  • @kulss, could you please create a new question? Post the link here that I'll sure take a look. It's kinda difficult to look your code in this way. – Lucas Eduardo Jun 16 '15 at 22:30