3 Answers
3

This is confusing, and something I've been trying to figure out how to properly document / design better, but you need to set your Accept header for the request to application/json.

Registering AFJSONRequestOperation says "any request that comes through, I'll look at and handle if it is a JSON request". The way you hint that a request is JSON is to either set the appropriate Accept header, or add .json as the extension. Right now, the request is going through the list of operation classes, but it only catches on AFHTTPRequestOperation, which has a response object type of NSData (what you're seeing in the log).

Thanks mattt! This really helped - now it works like a charm!
–
GoeranFeb 6 '12 at 17:07

2

If you don't have access to your server to change the response headers you can try the solution proposed by Ben H further down: id payload = [NSJSONSerialization JSONObjectWithData:JSON options:NSJSONWritingPrettyPrinted error:nil]; It worked for me.
–
SendoaJul 11 '13 at 8:46

Adding this line [httpClient setDefaultHeader:@"Accept" value:@"application/json"]; is giving me "An appropriate representation of the requested resource --- could not be found on this server". Is there another part of this?
–
Full DecentJul 5 '12 at 7:54

if you're using a custom accept header, like i am, AFNetworking won't recognize that you're requesting json, like mattt explained. one way around this is to simply convert the data into a json object in your completion block:

I couldn't change the servers response headers because I don't have access to it. The server was responding with "text/html" instead of "application/json". Your suggestion works like a charm. Thanks!!!
–
SendoaJul 11 '13 at 8:43

same here.. however I find it a little frustrating that all the classes using my rest client will have to call this json deserializing code.. i was hoping i could all lump all this config stuff into my REST singelton class
–
abboodSep 19 '13 at 2:28