3

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

Wain
  • 118,658
  • 15
  • 128
  • 151
foki
  • 65
  • 1
  • 5
  • Have you set the mime type that RestKit should use? And have you set request descriptors as well as response descriptors? – Wain Jun 10 '13 at 11:50

1 Answers1

2

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.

danielM
  • 2,462
  • 2
  • 20
  • 21