Request failed: unacceptable content-type: text/html using AFNetworking 2.0
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.
Related videos on Youtube
![jaytrixz](https://i.stack.imgur.com/uUT4r.jpg?s=256&g=1)
Comments
-
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 almost 11 yearsThanks! I just added this code to make it work:
op.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
-
jaytrixz over 10 yearsI made this and it crashes my app. Reverting back to using
AFJSONResponseSerializer
-
Danpe over 10 years@jaytrixz It depends, If your server always responds with JSON you should set the
responseSerializer
toAFJSONResponseSerializer
. -
rckehoe over 10 yearsFor 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 about 10 years@rckehoe Thanks for this - much preferred to change the page header than
acceptableContentTypes
:) -
Cameron Lowell Palmer about 10 yearsYou will now receive the responseObject as NSData and need to parse the JSON in the success block.
-
mgarciaisaia almost 10 yearsAn 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 almost 10 yearsIf 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 almost 10 yearsBecause I am using pods, so this way is better.
-
Husam over 9 yearsSwift code:
op.responseSerializer.acceptableContentTypes = NSSet(object: "text/html")
-
prakash singh over 9 yearsThanks dude! self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
-
Eric G over 9 yearsAgreed. 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 over 9 years
if(!headers_sent() ) { header('Content-Type: application/json'); }
Is a nice fix -
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 almost 9 yearsHey @G.Ganesh I updated my answer, try
manager.responseSerializer = AFHTTPResponseSerializer.serializer()
-
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 over 8 yearsAdding one more content type to the existing set is the possible way AFHTTPSessionManager_insatnce.responseSerializer.acceptableContentTypes = [AFHTTPSessionManager_insatnce.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
-
hklel over 8 yearsThe Swift version should be
manager.responseSerializer = AFHTTPResponseSerializer()
-
Brandon over 8 yearsExactly 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 over 8 yearsIf that happens in Parse.com platform that because you used the wrong keys such as restAPI key and not client key, etc
-
Andrew about 8 yearsSwift 2.2 is like the following
NSSet(object: "text/html") as? Set<String>
-
Ravi about 8 years@ Danpe, You are my Hero for the Day........ Thanks Buddy, works like a charm.....
-
Minimi over 7 yearsThis seems right in theory but has anyone actually tested this?