415 Restkit 响应中不支持的媒体类型

415 Unsupported Media Type in Restkit response

我正在尝试使用 Restkit 在 iOS 应用程序中调用休息 Web 服务,但出现此错误: restkit.network:RKObjectRequestOperation.m:210 response.body=415 不支持的媒体类型

415 不支持的媒体类型

我将这一行放在我的代码中以将内容类型设置为 application/json:

objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

这是我在控制台中显示的请求

request.headers={
    Accept = "application/json";
    "Accept-Language" = "en;q=1, fr;q=0.9, de;q=0.8, zh-Hans;q=0.7, zh-Hant;q=0.6, ja;q=0.5";
    Authorization = "Basic U1lTQURNSU46aHVsNTU4ODg1OA==";
    "Content-Type" = "application/json; charset=utf-8";
    "User-Agent" = "iosProj/1 (iPad Simulator; iOS 8.3; Scale/2.00)";
}

然后我发现问题可能是由here提到的"charset=utf-8"引起的。 我的问题是如何在 Restkit

中删除 "charset=utf-8"

感谢@Wain 的评论,我通过创建请求和设置内容类型解决了这个问题 header :

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];    
[request setHTTPBody:jsonData];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor
                                            responseDescriptorWithMapping: [LoginResponse getResponseMapping]
                                            method:RKRequestMethodPOST
                                            pathPattern:nil
                                            keyPath:@"OutputParameters"
                                            statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
RKObjectRequestOperation *operation = [[RKObjectRequestOperation alloc]initWithRequest:request responseDescriptors:@[responseDescriptor]];

我希望这对某人有所帮助。这件事最近发生在我身上,我也缺少 Content-Type。但是,您可以轻松地将其添加到 RKObjectManager 中:

RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:YOUR_BASE_URL]];
[manager setRequestSerializationMIMEType:RKMIMETypeJSON];