RestKit - 预期的内容类型为 application/json

4
我在使用RestKit时遇到了最奇怪的错误。以下是该错误的本地化描述 -
Expected content type {(
    "application/json",
    "text/html",
    "text/plain",
    "application/x-www-form-urlencoded"
)}, got {application/json}

你可以看到,我期望的内容类型是"application/json",而服务器返回的也是application/json。我不知道为什么会出现这个错误!

详细的错误信息在这里--

2015-02-19 11:24:43.234 MyProject[62944:10754151] E restkit.network:RKObjectRequestOperation.m:213 POST 'https://ipaddress/api/v1.0/user/verify' (200 OK / 0 objects) [request=0.1408s mapping=0.0000s total=0.1428s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
    "application/json",
    "text/html",
    "text/plain",
    "application/x-www-form-urlencoded"
)}, got {application/json" UserInfo=0x7f9c49db4930 {NSLocalizedRecoverySuggestion={"user_id":"3749f17f647c4ee1a9ee9d75c5f49f7e","domain":"domain","device_id":"2751029764","access_token":"589134b447974320b2d494a491e8226a","access_token_expiration":2181237828314,"refresh_token":"cb06c038be7b4f7c930de1fcb3f2017a"}, NSErrorFailingURLKey=https://ipaddress/api/v1.0/user/verify, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x7f9c49def0c0> { URL: https://ipaddress/api/v1.0/user/verify }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x7f9c49f567a0> { URL: https://ipaddress/api/v1.0/user/verify } { status code: 200, headers {
    "Baseline-Request-Id" = 30f91b50;
    Connection = "keep-alive";
    "Content-Length" = 230;
    "Content-Type" = "{application/json, q=1000}";
    Date = "Thu, 19 Feb 2015 19:23:48 GMT";
    Server = "nginx/1.6.2";
} }, NSLocalizedDescription=Expected content type {(
    "application/json",
    "text/html",
    "text/plain",
    "application/x-www-form-urlencoded"
)}, got {application/json}

更新--

经过一些调试,我发现问题在于响应中获取的“Content-Type”的值。如上面的示例所示,“Content-Type”的值为“{application/json, q=1000}”,而不是“application/json”。出于某种原因,RestKit将其读取为“{application/json”(省略逗号后的内容,并注意起始括号),因此无法与预期类型匹配。

为了以一种肮脏的方式“解决”这个问题,我添加了以下行(请注意“{”在“application/json”之前)

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"{application/json"];

但是我们都知道这不是正确的方法。我不知道这里有什么问题,是服务器发送了一些不正确的内容吗?还是RestKit应该以不同的方式读取它?


3
是的,服务器出现了错误,应该予以修复。您还应该将自己的解决方案添加为答案并接受它(或删除该问题)。 - Wain
1个回答

3

我无法找到服务器返回“错误”内容类型的原因。服务器的开发人员否认存在问题。因此,我最终采用了一种不太优雅的方法,在“application / json”之前添加了“{”字符。

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"{application/json"];

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接