Slee
Slee

Reputation: 28248

AFJSONRequestOperation failing because webservice returns JSON with content type text/html

I am trying to update my app to use AFNetworking but the webservice I need to work with returns JSON as text/html which is breaking when I try and use AFJSONRequestOperation:

Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html

Is there anyway to override this in AFNetworking so I can accept the text/html?

Upvotes: 3

Views: 1682

Answers (2)

Felix
Felix

Reputation: 35384

Yes it is possible, AFNetworking supports adding acceptable content types. In the init method of your AFHTTPClient subclass do the following:

[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];

I have had the same problem with my app and this made it work.

Upvotes: 13

Alejandro Benito-Santos
Alejandro Benito-Santos

Reputation: 2069

I don't think you should go that way unless you perfectly know what the framework innerly does . By experience, I can tell you possible would be breaking a lot of things if you patched it that badly plus, you'd have to maintain your own separate branch from that moment on, losing the benefits of updating to the latest version of AFNetworking in the future and thus, getting the latest bugfixes and features unless you integrated them into your branch.

If you just want it to work for now, fair enough, go ahead and try if it works (which I doubt). I'm sure there are other parts of AFNetworking that are expecting the content type to be "text/html", and by adding another one in a part that's not supposed to be you would have conflicts that would require you to patch more and more until the end of time.

Honestly, try to change the content-type your server is sending and do things as they're supposed to be done.

Another approach would be setting a "proxy" in your app (like a small server that runs locally in the phone) that retrieved the content and passed it to AFNetworking with the right content-type. Still, it'd be overcomplicating things for nothing but at least you wouldn't be modifying the framework yourself, which is the very last thing to do.

Upvotes: 0

Related Questions