0
votes

Let me start by saying I know the problem is the backend is the response, but there is nothing I can do about it. So...

I have a GET request that, on some cases, will send back a 200 response with an empty body. Again, can't change it.

Using Restkit, I have fairly every possible case properly mapped and everything works like a charm, but with this specific case I can't get it to be mapped as it should.

From everything I've seen, this seems to be the most accurate option to what I'm trying to do.

But, for some reason, the response descriptor doesn't seem to be picked up and the response is treated as a failure with the following error:

Error Domain=org.restkit.RestKit.ErrorDomain Code=-1017 "Loaded an unprocessable response (200) with content type 'application/json'"

Is there an 'easy' way to properly map an empty response, or even a string as response (clearly with no JSON structure whatsoever) ?

Thanks in advance for any light you can shed in my direction.

2
You said some cases, so do you have multiple response descriptors matching the same path pattern? You have the correct solution, the question is just why it isn't used... - Wain
Yeah, precisely. The GET request always sends 200 response: when there is an error it sends a JSON with related info; and when there is success it sends an empty body response. Currently I have both mappings and response descriptors (mapping to NSNull according to the link provided). The thing is, the empty response is never mapped to NSNull and I get the error mentioned. It's like the response descriptor is not considered. - Andres C

2 Answers

5
votes

As described in this post, there is an even better solution to your problem.

You can simply add an ResponseDescriptor with an ObjectMapping to NSNull:

// Create RKObjectMapping for NSNull:
RKObjectMapping* responseMapping = [RKObjectMapping mappingForClass:[NSNull class]];

// Create RKResponseDescriptor with mapping:
RKResponseDescriptor* response = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                              method:RKRequestMethodAny
                                                                         pathPattern:@"/yourPathPattern"       
                                                                             keyPath:nil
                                                                         statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];


// Add RKResponseDescriptor to RKObjectManager:
[[RKObjectManager sharedManager] addResponseDescriptor:response];
1
votes

You should not have 2 response descriptors in this case, you should only have one. That one response descriptor is linked to a dynamic mapping which analyses the response and returns the appropriate mapping (from your 2 existing mappings).