Veeru
Veeru

Reputation: 4936

Restkit cannot read valid json?

am just starting off with restkit for ios. I have a very simple json response

{ "result": [ { "userid": "5964", "name": "Your Name" } ] }

This is a valid json (tested against jsonlint)

However, when i try to use this with RKClient, it doe'snt see it as json. This is my code

-(void) request:(RKRequest *)request didLoadResponse:(RKResponse *)response{

    if([request isGET]){
        if([response isJSON]){
            NSLog(@"JSON Response from Server %@", [response bodyAsString] );    
        }else{
            NSLog(@"Non JSON Response from Server %@", [response bodyAsString] );    
        }
    }

}

It always logs "Non JSON Reponse...." but the json is perfectly valid. The same thing happens when i use RKObjectMapping; the error will be "Unable to find parser for MIME Type text/xml.

Any help is highly appreciated.

Thanks

Upvotes: 1

Views: 1533

Answers (3)

Antoine Beloeuvre
Antoine Beloeuvre

Reputation: 526

If for any reason you can't change the server headers and returned Mime type to xml or json, you can still add mapping for mime type 'text/html' to be processed as json.

Just add :

[[RKParserRegistry sharedRegistry] setParserClass:[RKJSONParserJSONKit class] forMIMEType:@"text/html"];   

And don't forget to add the related header file :

#import <RestKit/RKJSONParserJSONKit.h>

Works for me.

Have a look at https://github.com/RestKit/RestKit/wiki/Using-a-Custom-MIME-Type

Upvotes: 9

Beber
Beber

Reputation: 1513

Serveur response header should be "application/json", and not "text/xml". ;p

Upvotes: 3

Veeru
Veeru

Reputation: 4936

I was a bit dumb, but things got solved once i added

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

to the server script.

Upvotes: 2

Related Questions