RestKit 0.2 错误 '期望状态码在 (400-499),而实际得到的是 200'

4
我正在使用RestKit 0.2,出现以下错误:
E restkit.network:RKObjectRequestOperation.m:237 GET 'http://some.url.com/o/3134' (200 OK / 0 objects) [request=3.2563s mapping=0.0000s total=3.2955s]: Error Domain=org.restkit.RestKit.ErrorDomain Code=-1011 "Expected status code in (400-499), got 200" UserInfo=0x87712e0 {NSLocalizedRecoverySuggestion={"d":"2013-07-15T02:30:00.000Z","t":16.1,"at":13.8}, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest http://some.url.com/o/3134>, NSErrorFailingURLKey=http://some.url.com/o/3134, NSLocalizedDescription=Expected status code in (400-499), got 200, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x9647260>}

我使用Node.js和Restify作为服务器端,能够通过Google Chrome获取JSON数据,我可以在Chrome中看到响应的Content-Type是application/json。
但当我使用RestKit时,出现了“Expected status code in (400-499), got 200”的错误,我认为200是正常的,为什么RestKit期望的是400-499而不是200呢?并且我也可以在错误消息中看到JSON对象{"d":"2013-07-15T02:30:00.000Z","t":16.1,"at":13.8}。
谢谢。

展示你的映射和响应描述符。状态码在那里设置,所以某些东西不对齐。 - Wain
1
@Wain 谢谢,我发现这是一个映射问题。请看 https://github.com/RestKit/RestKit/issues/1501 - Jake Lin
1个回答

6

当您为请求定义响应描述符时,如果您期望一个HTTP状态码为2xx的响应,则需要将状态码设置为RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)。

示例:

[RKResponseDescriptor responseDescriptorWithMapping:yourMapping method:RKRequestMethodAny pathPattern:yourPattern keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

更新:

请确保响应描述符(response descriptor)的路径模式(pathPattern)与请求路径匹配。否则,RestKit将使用错误响应描述符(如果您定义了它),并期望具有不同状态代码的响应。


1
谢谢!我的问题是由于你提到的路径模式不匹配引起的。修复了这个问题后,我的情况得到了解决。 - Clay Garrett

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