Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is RestKit changing my response content-type?

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

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
}];

the error i get is:

 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

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?

p.s. I don't have control over the server, so I can't change it's content-type header to application/JSON


Update:

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;

how do I use this to register a text/html content-type?

like image 380
abbood Avatar asked Oct 26 '13 07:10

abbood


1 Answers

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"];

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.

Hope this helps.

like image 191
Kyle Clegg Avatar answered Jan 02 '23 07:01

Kyle Clegg