次の JSON があり、そのルーティング マッピングも続きます。単一アイテムの JSON
{
"quantity" : 0,
"id" : 1,
"version" : 0,
"sku" : "sku1",
"title" : "title1",
}
すべての項目の JSON は次のとおりです。
[
{
"quantity" : 2,
"id" : 1,
"version" : 0,
"sku" : "sku1",
"title" : "title1",
},
{
"quantity" : 4,
"id" : 2,
"version" : 0,
"sku" : "sku2",
"title" : "title2",
}
]
マッピングは次のとおりです。
RKEntityMapping *newItemMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Item class]) inManagedObjectStore:manager.managedObjectStore];
newItemMapping.identificationAttributes = @[@"id"];
[newItemMapping addAttributeMappingsFromDictionary:@{
@"id" : @"id",
@"version" : @"version",
@"title" : @"title",
@"sku" : @"sku"
}];
ルーティング情報はこちら。
[[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]];
GET リクエストは正常に機能し、上記のマッピングを使用してオブジェクトを正しくロードします。
[[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);
}];
しかし、POST リクエストは以下のメッセージで失敗します:-
[[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);
}];
エラーメッセージ:-
投稿の保存に失敗しました: 予想されるコンテンツ タイプ {( "application/x-www-form-urlencoded", "application/json" )}、取得した text/html
何が問題なのかを修正するのを手伝ってください?
ありがとう、フォキ