我正在尝试扩展现有应用以存储特定对象服务器端的用户收藏夹。
该应用正在使用RestKit~> 0.26.0,它使用响应描述符设置了许多GET调用。但是,当我为POST / DELETE调用的请求描述符进行逆映射时,它似乎无法导致生成任何请求体。
请求描述符设置代码:
// Code User Favorites Mapping
RKEntityMapping *codeFavoritesMapping = [RKEntityMapping mappingForEntityForName:@"Code" inManagedObjectStore:objectStore];
[codeFavoritesMapping setIdentificationAttributes:@[@"scheme", @"code"]];
[codeFavoritesMapping addAttributeMappingsFromArray:@[ @"cuid", @"code", @"scheme" ]];
[codeFavoritesMapping addAttributeMappingsFromDictionary:@{ @"shortdesc" : @"text" }];
// POST/DELETE
[[RKObjectManager sharedManager] addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:[codeFavoritesMapping inverseMapping]
objectClass:[Code class]
rootKeyPath:nil
method:(RKRequestMethodPOST | RKRequestMethodDELETE)]];
// GET
[[RKObjectManager sharedManager] addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:codeFavoritesMapping
method:RKRequestMethodGET
pathPattern:kRestKitEndpointCodeFavorites
keyPath:API_WRAPER_KEYPATH
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];
请注意,上面设置的GET按预期运行。
以下是POST / DELETE调用本身:
if (isFavorite) { // Removing from favorite list
...
// Delete favorite server side
[[RKObjectManager sharedManager] deleteObject:_code path:kRestKitEndpointCodeFavorites parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Deleted code: %@", _code.code);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed to delete code: %@", _code.code);
}];
} else { // Adding to favorite list
...
// Add favorite server side
[[RKObjectManager sharedManager] postObject:_code path:kRestKitEndpointCodeFavorites parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"Added code: %@", _code.code);
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"Failed to add code: %@", _code.code);
}];
}
根据我的理解,这应该导致POST / DELETE调用发送一个类似的请求体:
{"code":"101","scheme":"scheme1","cuid":"123456789","shortdesc":"Basic code sample"}
然而,服务器和RestKit日志显示没有任何内容被发送。也许我错过了一些基本的东西?
让我知道您认为可能是什么问题,或者您是否需要进一步的信息/澄清。谢谢你的时间!
编辑:添加RestKit日志信息
POST:
I restkit.network:RKObjectRequestOperation.m:137 POST 'https://mobile-api.myapp.com:8000/v2/codes'
D restkit.network:RKObjectRequestOperation.m:138 request.headers={
Accept = "application/json";
"Accept-Language" = "en-US;q=1";
Authorization = "Basic ZHJ1bXBsZW1kaHRtbDpzY3JpYmU=";
"User-Agent" = "MyApp/1.2.3.4 (iPod touch; iOS 9.3.2; Scale/2.00)";
}
I restkit.network:RKResponseMapperOperation.m:515 Non-successful status code encountered: performing mapping with nil target object.
E restkit.network:RKObjectRequestOperation.m:175 POST 'https://mobile-api.myapp.com:8000/v2/codes' (400 Bad Request / 0 objects) [request=3.4962s mapping=0.0000s total=3.5758s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=1001 "No mappable object representations were found at the key paths searched." UserInfo={DetailedErrors=(
), NSLocalizedFailureReason=The mapping operation was unable to find any nested object representations at the key paths searched:
The representation inputted to the mapper was found to contain nested object representations at the following key paths: code, developerMessage, message, moreInfo, property, status
This likely indicates that you have misconfigured the key paths for your mappings., NSLocalizedDescription=No mappable object representations were found at the key paths searched., keyPath=null}
T restkit.network:RKObjectRequestOperation.m:182 response.body={"status":400,"code":0,"property":"","message":"Invalid data","developerMessage":"invalid scheme","moreInfo":""}
Failed to add code: 101
DELETE:
I restkit.network:RKObjectRequestOperation.m:137 DELETE 'https://mobile-api.myapp.com:8000/v2/codes'
D restkit.network:RKObjectRequestOperation.m:138 request.headers={
Accept = "application/json";
"Accept-Language" = "en-US;q=1";
Authorization = "Basic ZHJ1bXBsZW1kaHRtbDpzY3JpYmU=";
"User-Agent" = "MyApp/1.2.3.4 (iPod touch; iOS 9.3.2; Scale/2.00)";
}
I restkit.network:RKResponseMapperOperation.m:515 Non-successful status code encountered: performing mapping with nil target object.
I restkit.network:RKObjectRequestOperation.m:178 DELETE 'https://mobile-api.myapp.com:8000/v2/codes' (400 Bad Request / 0 objects) [request=2.0675s mapping=0.0057s total=2.1261s]
D restkit.network:RKObjectRequestOperation.m:179 response.headers={
Connection = close;
"Content-Length" = 112;
"Content-Type" = "application/json; charset=utf-8";
Date = "Fri, 24 Jun 2016 17:22:09 GMT";
Etag = "W/\"70-nu03kd5Vpszkdw25X+pmaQ\"";
Vary = "Accept-Encoding";
"x-powered-by" = Express;
}
T restkit.network:RKObjectRequestOperation.m:182 response.body={"status":400,"code":0,"property":"","message":"Invalid data","developerMessage":"invalid scheme","moreInfo":""}
Deleted code: 101
编辑:回应Wain的问题
我已经检查了服务器收到了什么,但它只是一个空的请求主体发送。我已经尝试在两个POST / DELETE调用的参数部分中传递对象的字典,手动编码。
对POST参数进行手动编码会按预期发送,并且生成的请求正文完全符合要求。但是,手动编码DELETE调用的结果会导致手动编码参数成为url查询字符串的一部分;即https://mobile-api.myapp.com:8000/v2/codes?code=101&scheme=scheme1&cuid=123456789&shortdesc=Basic code sample
RestKit MIME类型设置:
RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:API_HOST_NAME]];
[RKObjectManager setSharedManager:objectManager];
[objectManager setRequestSerializationMIMEType:RKMIMETypeJSON];
[objectManager setAcceptHeaderWithMIMEType:RKMIMETypeJSON];