Request failed: unacceptable content-type: text/html using AFNetworking 2.0

176,652

Solution 1

This means that your server is sending "text/html" instead of the already supported types. My solution was to add "text/html" to acceptableContentTypes set in AFURLResponseSerialization class. Just search for "acceptableContentTypes" and add @"text/html" to the set manually.

Of course, the ideal solution is to change the type sent from the server, but for that you will have to talk with the server team.

Solution 2

Setting my RequestOperationManager Response Serializer to HTTPResponseSerializer fixed the issue.

Objective-C

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

Swift

manager.responseSerializer = AFHTTPResponseSerializer()

Making this change means I don't need to add acceptableContentTypes to every request I make.

Solution 3

I took @jaytrixz's answer/comment one step further and added "text/html" to the existing set of types. That way when they fix it on the server side to "application/json" or "text/json" I claim it'll work seamlessly.

  manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

Solution 4

On the server side, I added:

header('Content-type: application/json');

into my .php code and this also fixed the problem.

Solution 5

I solve this problem from a different perspective.

I think if the server sends JSON data with Content-Type: text/html header. It doesn't mean the server guy intended to send you some html but accidentally changed to JSON. It does mean the server guy just doesn't care about what the Content-Type header is. So if the server guy doesn't care as the client side you better ignore the Content-Type header as well. To ignore the Content-Type header check in AFNetworking

manager.responseSerializer.acceptableContentTypes = nil;

In this way the AFJSONResponseSerializer (the default one) will serialize the JSON data without checking Content-Type in response header.

Share:
176,652

Related videos on Youtube

jaytrixz
Author by

jaytrixz

Filipino  fan

Updated on June 21, 2020

Comments

  • jaytrixz
    jaytrixz about 4 years

    I'm trying out the new version 2.0 of AFNetworking and I'm getting the error above. Any idea why this is happening? Here's my code:

        NSURL *URL = [NSURL URLWithString:kJSONlink];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];
        AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
        op.responseSerializer = [AFJSONResponseSerializer serializer];
        [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"JSON: %@", responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"Error: %@", error);
        }];
        [[NSOperationQueue mainQueue] addOperation:op];
    

    I'm using Xcode 5.0.

    Also, here's the error message:

    Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0xda2e670 {NSErrorFailingURLKey=kJSONlink, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xda35180> { URL: kJSONlink } { status code: 200, headers {
        Connection = "Keep-Alive";
        "Content-Encoding" = gzip;
        "Content-Length" = 2898;
        "Content-Type" = "text/html";
        Date = "Tue, 01 Oct 2013 10:59:45 GMT";
        "Keep-Alive" = "timeout=5, max=100";
        Server = Apache;
        Vary = "Accept-Encoding";
    } }, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}
    

    I just hid the JSON using kJSONlink. This should return a JSON.

  • jaytrixz
    jaytrixz almost 11 years
    Thanks! I just added this code to make it work: op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
  • jaytrixz
    jaytrixz over 10 years
    I made this and it crashes my app. Reverting back to using AFJSONResponseSerializer
  • Danpe
    Danpe over 10 years
    @jaytrixz It depends, If your server always responds with JSON you should set the responseSerializer to AFJSONResponseSerializer.
  • rckehoe
    rckehoe over 10 years
    For PHP it's as easy as adding this to the page: header("Content-Type: application/json"); (unless it's not a JSON response, then XML or something)
  • Nick
    Nick about 10 years
    @rckehoe Thanks for this - much preferred to change the page header than acceptableContentTypes :)
  • Cameron Lowell Palmer
    Cameron Lowell Palmer about 10 years
    You will now receive the responseObject as NSData and need to parse the JSON in the success block.
  • mgarciaisaia
    mgarciaisaia almost 10 years
    An alternative to @jaytrixz comment is to just add a new content type to the already existing ones: op.responseSerializer.acceptableContentTypes = [op.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
  • vishal dharankar
    vishal dharankar almost 10 years
    If you are dealing with standard API you will never have to do that, adding text/html as a type and sending JSON is bad practice. Changing server code is proper solution.
  • yong ho
    yong ho almost 10 years
    Because I am using pods, so this way is better.
  • Husam
    Husam over 9 years
    Swift code: op.responseSerializer.acceptableContentTypes = NSSet(object: "text/html")
  • prakash singh
    prakash singh over 9 years
    Thanks dude! self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
  • Eric G
    Eric G over 9 years
    Agreed. The accepted answer to this question has a huge flaw in that it creates a time bomb that will explode when the server side is fixed to return the correct content type.
  • elliotrock
    elliotrock over 9 years
    if(!headers_sent() ) { header('Content-Type: application/json'); } Is a nice fix
  • Ganesh G
    Ganesh G almost 9 years
    @Danpe, How to convert above line of code into Swift. I tried with manager.responseSerializer = AFJSONResponseSerializer.serializer() but no use.
  • Danpe
    Danpe almost 9 years
    Hey @G.Ganesh I updated my answer, try manager.responseSerializer = AFHTTPResponseSerializer.serializer()
  • loretoparisi
    loretoparisi almost 9 years
    +1. This is definitively the solution. Since you have to set the acceptable Content Type both on the Serializer if this is a JSON serializer (that is normally using "application/json") and the operation. Otherwise you will get an error settings on operation only.
  • Rahul K Rajan
    Rahul K Rajan over 8 years
    Adding one more content type to the existing set is the possible way AFHTTPSessionManager_insatnce.responseSerializer.acceptableC‌​ontentTypes = [AFHTTPSessionManager_insatnce.responseSerializer.acceptable‌​ContentTypes setByAddingObject:@"text/html"];
  • hklel
    hklel over 8 years
    The Swift version should be manager.responseSerializer = AFHTTPResponseSerializer()
  • Brandon
    Brandon over 8 years
    Exactly correct. By ignoring the content type I do not receive my content as a hex code, nor as a failed nil response. This works great! Thank you
  • dang
    dang over 8 years
    If that happens in Parse.com platform that because you used the wrong keys such as restAPI key and not client key, etc
  • Andrew
    Andrew about 8 years
    Swift 2.2 is like the following NSSet(object: "text/html") as? Set<String>
  • Ravi
    Ravi about 8 years
    @ Danpe, You are my Hero for the Day........ Thanks Buddy, works like a charm.....
  • Minimi
    Minimi over 7 years
    This seems right in theory but has anyone actually tested this?