Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestKit 0.20.2 Routing and mapping

I have the following JSON and routing mapping for it also follows. Single Item JSON

{
"quantity" : 0,
"id" : 1,
"version" : 0,
"sku" : "sku1",
"title" : "title1",
}

and all Items JSON is as follows:-

[
    {
    "quantity" : 2,
    "id" : 1,
    "version" : 0,
    "sku" : "sku1",
    "title" : "title1",
    },
    {
    "quantity" : 4,
    "id" : 2,
    "version" : 0,
    "sku" : "sku2",
    "title" : "title2",
    }
]

Here are the mappings:-

RKEntityMapping *newItemMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Item class]) inManagedObjectStore:manager.managedObjectStore];
newItemMapping.identificationAttributes = @[@"id"];
[newItemMapping addAttributeMappingsFromDictionary:@{
        @"id" : @"id",
        @"version" : @"version",
        @"title" : @"title",
        @"sku" : @"sku"
}];

Here is the routing info.

[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items/:id" method:RKRequestMethodGET]];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items" method:RKRequestMethodPOST]];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items/:id" method:RKRequestMethodPUT]];
[[RKObjectManager sharedManager].router.routeSet addRoute:[RKRoute routeWithClass:[Item class] pathPattern:@"items/:id" method:RKRequestMethodDELETE]];

The GET request works fine and loads the object correctly using above mapping.

[[RKObjectManager sharedManager] getObject:sampleItemObject path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    RKLogInfo(@"Load collection of Items: %@", mappingResult);
}                                         failure:^(RKObjectRequestOperation *operation, NSError *error) {
    RKLogError(@"Operation failed with error: %@", error);
}];

But the POST request fails with the message shown below:-

[[RKObjectManager sharedManager] postObject:newItem path:nil parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"%@", mappingResult.array);
}                                             failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Failure saving post: %@", error.localizedDescription);
}];

Error message:-

Failure saving post: Expected content type {( "application/x-www-form-urlencoded", "application/json" )}, got text/html

Please assist in fixing whatever is going wrong?

Thanks, Foki

like image 686
foki Avatar asked Jan 29 '26 05:01

foki


1 Answers

Add this line when you configure your shared manager or at some point before you make the post request:

[RKObjectManager sharedManager].requestSerializationMIMEType = RKMIMETypeJSON;

Also, make sure you have a serialization mapping defined for your object. These will allow RestKit to send the object to the server as a properly formatted JSON string.

like image 104
danielM Avatar answered Jan 31 '26 23:01

danielM



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!