ios - 为什么 RestKit 会改变我的响应内容类型?

标签 ios objective-c restkit afnetworking afnetworking-2

简而言之:我尝试从服务器获取数据,并将 http 请求 header 的 content-type 设置为 @"text/html.. 但对于某些RestKit 将其更改为 application/JSON

的原因

解释: 如果我只使用 AFNetworking 提出这个请求.. 一切都很顺利.. 这就是我的 AFNetworking 代码的样子:

AFHTTPClient *client = [AFHTTPClient alloc] initWithBaseURL:
                                               [NSURL URLWithString:kApiBaseUrl]];
singleton.parameterEncoding = AFJSONParameterEncoding;
[singleton setDefaultHeader:@"Accept" value:@"text/html"];
[client getPath:getPath parameters:nil success:successCallback failure:failureCallback];

如果我使用那个完全相同的客户端并将其附加到

MyClient *client = [MyClient getSingleton];  //MyClient is instantiated as above        
self.objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
self.objectManager.managedObjectStore = self.managedObjectStore;
// this should have already been done by my client, but putting
// it here just to be sure
[self.objectManager setAcceptHeaderWithMIMEType:@"text/html"];

[[RKObjectManager sharedManager] getObjectsAtPath:kGradesPath 
                                       parameters:nil 
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
 // handle success
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
 // handle failure
}];

我得到的错误是:

 restkit.network:RKObjectRequestOperation.m:576 Object request failed: 
 Underlying HTTP request operation failed with error: Error
 Domain=org.restkit.RestKit.ErrorDomain Code=-1016 "Expected content type {(
    "application/x-www-form-urlencoded",
    "application/json"
)}, got text/html" UserInfo=0x8f7acd0

深入探讨这个主题.. 我在 managedObjectRequestOperationWithRequest 中设置了一个断点, 然后我检查了 acceptableContentTypes HTTPRequestOperation 已创建,但为零!所以我假设 RestKit 只是把它自己默认的可接受的内容类型.. 我只是不知道在哪里以及如何阻止它。想法?

p.s. 我无法控制服务器,所以我无法将其 content-type header 更改为 application/JSON


更新:

事实证明,在RKObjectRequestOperation.m它从 [RKMIMETypeSerialization registeredMIMETypes];(第 354 行)获取 mime-type.. 等等 RKMIMETypeSerialization.h有方法:

/**
 Registers the given serialization class to handle content for the given MIME Type identifier.

 MIME Types may be given as either a string or as a regular expression that matches the MIME Types for which the given serialization should handle. Serializations are searched in the reverse order of their registration. If a registration is made for an already registered MIME Type, the new registration will take precedence.

 @param serializationClass The class conforming to the RKSerialization protocol to be registered as handling the given MIME Type.
 @param MIMETypeStringOrRegularExpression A string or regular expression specifying the MIME Type(s) that given serialization implementation is to be registered as handling.
 */
+ (void)registerClass:(Class<RKSerialization>)serializationClass forMIMEType:(id)MIMETypeStringOrRegularExpression;

我如何使用它来注册一个 text/html 内容类型?

最佳答案

RestKit 通常期望其响应数据使用单个 MIMEType (JSON)。但是,您可以使用您找到的方法告诉它处理其他类型,如 text/plaintext/html,根据我的经验,它非常方便。将此添加到我的 RestKit 配置(我在我的应用程序委托(delegate)中这样做)允许我接受 application/jsontext/html 作为响应数据内容类型。

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];

在我的例子中,这也很有帮助,因为 Jersey——我公司后端团队使用的 Web 服务框架——默认空负载的内容类型为 text/plain,这会触发失败 block 除非我专门注册了那个 MIMEType。

希望这对您有所帮助。

关于ios - 为什么 RestKit 会改变我的响应内容类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19603976/

相关文章:

iOS - 获取当前 AVPlayer(Item) 状态

ios - UICollectionView 中的 UITableView

ios - 在函数 swift 中输入 **nil**

ios - 为什么将其[__NSCFString objectAtIndex:]:无法识别的选择器发送到实例?

objective-c - RestKit 对象映射 : difficulty using setObjectMapping:forResourcePathPattern:withFetchRequestBlock

ruby-on-rails - Rails 从 RestKit POST 显示 "WARNING: Can' t verify CSRF token authenticity

iphone - 从另一个窗口中打开已经创建的窗口

ios - 将特定注释的数据带到下一个 View

iphone - 将 NSDATA 转换为 UIImage 时速度慢

ios - RestKit 将对象 POST 到服务器,但服务器收到空的 GET 请求