我整天都有這個問題。我正在使用RestKit,並且已經成功地發出GET請求並將所有信息映射到對象中。下面是一段代碼片段,其中我提出了GET請求,然後是POST請求(用於測試)。我將剛收到的對象再次發佈到服務器。但!我的POST請求失敗。我嘗試用Chrome插件Postman測試REST API,確認REST API工作正常。下面是這使得GET和POST請求的代碼:RestKit:POST錯誤(加載了無法處理的錯誤響應(400))
- (void)loadSample
{
[[RKObjectManager sharedManager] getObjectsAtPath:@"sampling/243570" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
Sample *obj = [mappingResult.array firstObject];
[[SampleList sharedInstance] addSample:obj];
[self.tableView reloadData];
[[RKObjectManager sharedManager] postObject:obj path:@"sampling" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
NSLog(@"POST: Success");
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
NSLog(@"POST: Failure");
}];
} failure:nil];
}
我碰到下面的調試消息時嘗試後:
restkit.network:RKObjectRequestOperation.m:208 POST 'https://XXXX.dk/api/sampling'( 400錯誤請求/ 0對象)[request = 0.0258s mapping = 0.0000s total = 0.0320s]: error =錯誤域= org.restkit.RestKit.ErrorDomain代碼= -1016「預期內容類型{( 」application/x -www-form-urlencoded「, 」application/json「 )},得到text/plain」UserInfo = 0x10a81f5d0 {NSLo calizedRecoverySuggestion =,NSErrorFailingURLKey = https://XXXX.dk/api/sampling,AFNetworkingOperationFailingURLRequestErrorKey = {URL:https://XXXX.dk/api/sampling},AFNetworkingOperationFailingURLResponseErrorKey = {URL:https://XXXX.dk/api/sampling} {狀態碼:400,標頭{ 「緩存控制」= 「無緩存」; 「Content-Length」= 0; 日期=「星期四,2014年7月24日17:21:57 GMT」; Expires =「-1」; Pragma =「no-cache」; Server =「Microsoft-IIS/8.0」; 「X-AspNet-Version」=「4.0.30319」; 「X-Powered-By」=「ASP.NET」; }},NSLocalizedDescription =預期的內容類型{( 「應用程序/ x WWW的形式進行了urlencoded」, 「應用/ JSON」 )},得到純文本/} response.body =
所以我添加的方法如下行:configureRestKit(見下表)
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
,並得到了下面的調試消息我POST請求。看了一整天,並看不到我在錯誤期間,因爲它處理我的GET請求完美(所以我不認爲我的映射是不正確的):
2014-07-24 18:46:21.199 XXXX [1492:60B] T,restkit.network:RKObjectRequestOperation.m:148 POST 'https://XXXX.dk/api/sampling':
request.headers = { 「接受 - 語言」=「DA; q = 1,連接; q = 0.9,fr; q = 0.8,de; q = 0.7,zh-Hans; q = 0.6,zh-Hant; q = 0.5「; Authorization = XXXX.dk; 「Content-Type」=「application/json」; 「User-Agent」=「XXXX/1.0(iPhone Simulator; iOS 7.1; Scale/2.00)」; }
request.body =(空)
2014年7月24日18:46:21.234 XXXX.dk [1492:F03]Èrestkit.network:RKObjectRequestOperation.m:208 POST 'https://XXXX.dk/api/sampling'( 400 Bad Request/0 objects)[request = 0.0343s mapping = 0.0000s total = 0.0405s]: error = Error Domain = org.restkit.RestKit.ErrorDomain Code = -1011「加載了一個無法處理的錯誤響應(400)」UserInfo = 0x10a6b5780 {NSLocalizedDescription =加載了一個無法處理的錯誤響應(400),NSErrorFailingURLKey = https://XXXX.dk/api/sampling} 響應。體=
我使用RestKit的配置如下:
- (void)configureRestKit
{
// initialize AFNetworking HTTPClient
NSURL *baseURL = [NSURL URLWithString:@"https://XXXX.dk/api/"];
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
// initialize RestKit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
[objectManager.HTTPClient setDefaultHeader:@"Content-Type" value:@"application/json"];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/plain"];
// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor =
[RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider sampleMapping]
method:RKRequestMethodGET
pathPattern:nil
keyPath:nil
statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[objectManager addResponseDescriptor:responseDescriptor];
}
[MappingProvider sampleMapping]是返回以下結果:
+ (RKObjectMapping *) sampleMapping {
RKObjectMapping *sampleMapping = [RKObjectMapping mappingForClass:[Sample class]];
[sampleMapping addAttributeMappingsFromDictionary:@{
@"SampleId": @"sampleId",
@"Type": @"type",
@"Scope": @"scope",
@"SamplingMethod": @"samplingMethod"
}];
[sampleMapping addPropertyMapping:[MappingProvider fieldAnalysisMapping]];
[sampleMapping addPropertyMapping:[MappingProvider sampleDataMapping]];
[sampleMapping addPropertyMapping:[MappingProvider laboratoryAnalysisMapping]];
return sampleMapping;
}
您是否有請求描述符? – Wain
不 - 我讀過框架會自動反轉RKResponseDescriptor將其映射回JSON?我不確定它是否正確。 – SOK
你從哪裏讀到的?這不是真的。 – Wain