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

abb*_*ood 7 objective-c ios restkit afnetworking afnetworking-2

简而言之:我尝试content-type从http请求标头设置为服务器的服务器中获取数据@"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];
Run Code Online (Sandbox Code Playgroud)

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

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
}];
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

 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
Run Code Online (Sandbox Code Playgroud)

深入挖掘主题..我把一个破发点managedObjectRequestOperationWithRequest,然后我检查了acceptableContentTypesHTTPRequestOperation创建,它是零!所以我假设RestKit只是放置自己的默认可接受的内容类型..我只是不知道在哪里以及如何防止它.想法?

ps我无法控制服务器,因此无法将其content-type标题更改为application/JSON


更新:

事实证明,在RKObjectRequestOperation.m中它获取了mime-typefrom [RKMIMETypeSerialization registeredMIMETypes];(第354行)..所以在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;
Run Code Online (Sandbox Code Playgroud)

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

Kyl*_*egg 29

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

[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
Run Code Online (Sandbox Code Playgroud)

在我的情况下,这也很有用,因为泽西岛 - 我公司后端团队使用的Web服务框架 - 将空载荷的内容类型默认为text/plain,除非我专门为该MIMEType注册,否则会触发失败块.

希望这可以帮助.