【问题标题】:415 Unsupported Media Type in Restkit response415 Restkit 响应中不支持的媒体类型
【发布时间】:2016-05-11 15:35:00
【问题描述】:

我正在尝试使用 Restkit 在 iOS 应用程序中调用 REST 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”

【问题讨论】:

  • 我猜你需要让 RestKit 创建 NSURLRequest 然后明确设置新的内容类型标题
  • 谢谢@Wain,它起作用了:NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; [请求 setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
  • 将其添加为答案并接受它:)

标签: ios objective-c json restkit


【解决方案1】:

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

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]];

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2014-05-10
      • 2019-05-01
      • 2017-06-30
      • 2017-07-05
      • 2016-11-26
      • 2015-08-21
      • 2015-08-29
      相关资源
      最近更新 更多