为什么RestKit会改变我的响应内容类型? [英] why is RestKit changing my response content-type?
问题描述
简而言之:我尝试从http请求标头的内容类型
设置服务器获取数据 @text / html
..但由于某些原因,RestKit将其更改为 application / JSON
In short: I try to fetch data form the server with the content-type
of the http request header set as @"text/html
.. but for some reason RestKit changes that to application/JSON
说明:
如果我只使用 AFNetworking 来发出此请求..事情就像一个魅力..这就是我的AFNetworking代码:
Explanation: If I were to make this request using just AFNetworking.. things work like a charm.. this is what my AFNetworking code looks like:
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];
如果我使用完全相同的客户端并将其附加到
If I use that exact same client and attach it to
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 ,然后我检查了 HTTPRequestOperation 的blob / development / Code / Network / RKHTTPRequestOperation.h?source = c> acceptableContentTypes ,它是零!所以我假设RestKit只是放置自己的默认可接受的内容类型..我只是不知道在哪里以及如何防止它。想法?
digging into the subject.. i put a break point in managedObjectRequestOperationWithRequest, then I checked the acceptableContentTypes of HTTPRequestOperation
created, and it's nil! So i'm assuming that RestKit is just putting its own default acceptable content types.. i just don't know where and how to prevent it. ideas?
ps 我无法控制服务器,所以我无法更改它的 content-type
标题为 application / JSON
p.s. I don't have control over the server, so I can't change it's content-type
header to application/JSON
更新:
事实证明,在 RKObjectRequestOperation.m 从获取
(第354行)..以及 RKMIMETypeSerialization.h 有方法: mime-type
[RKMIMETypeSerialization registeredMIMETypes] ;
It turns out that in RKObjectRequestOperation.m it gets the mime-type
from [RKMIMETypeSerialization registeredMIMETypes];
(line 354).. and so in RKMIMETypeSerialization.hthere is the method:
/**
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
content-type?
how do I use this to register a text/html
content-type?
推荐答案
RestKit通常需要一个MIMEType(JSON)作为其响应数据。但是,您可以使用您找到的方法告诉它处理其他类型,如 text / plain
和 text / html
根据我的经验,它非常方便。将此添加到我的RestKit配置(我在我的应用程序委托中执行)允许我接受 application / json
和 text / html
作为响应数据内容类型。
RestKit generally expects a single MIMEType (JSON) for its response data. However, you can tell it to handle other types like text/plain
and text/html
using the method you found and in my experience it's been pretty handy. Adding this to my RestKit config (which I do in my app delegate) allows me to accept both application/json
and text/html
as response data content-types.
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];
在我的情况下,这也很有用,因为泽西岛 - 我公司的后端团队的Web服务框架uses - 将空有效负载的内容类型默认为 text / plain
,除非我专门为该MIMEType注册,否则会触发失败块。
In my case, this is also helpful because Jersey - the web services framework the backend team at my company uses - defaults the content-type of empty payloads to text/plain
, which triggers failure blocks unless I've specifically registered for that MIMEType.
希望这会有所帮助。
这篇关于为什么RestKit会改变我的响应内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!