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