When I first started experimenting with AFNetworking 2.0 in Objective-C, I thought making an HTTP GET request and parsing JSON would be a straightforward task. But like many developers new to this library, I quickly ran into a frustrating error:
Request failed: unacceptable content-type: text/html
I wasn’t sure what was wrong. The server was supposed to return JSON, but AFNetworking kept complaining about the content type. If you’ve faced this issue, you’re not alone. Let me walk you through what I learned, why the error occurs, and how I fixed it with improved functionality that can help you grow your confidence using AFNetworking in real-world projects.
The My Code
Here’s the code snippet I started with:
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 expected this to simply fetch and log JSON. But instead, I hit a wall.
The Error I Got
Error: Request failed: unacceptable content-type: text/html
Explanation
AFNetworking uses a response serializer to automatically check if the server is returning data in the format you asked for. Since I was using AFJSONResponseSerializer
, it was expecting a response with:
Content-Type: application/json
But the server responded with:
Content-Type: text/html
This can happen when:
- You’re requesting the wrong endpoint (e.g. the server returns a 404 HTML page).
- The server is returning an error page formatted as HTML.
- The API is actually sending JSON but mislabeled it as
text/html
.
How I Fixed It
Fix the Server
Ideally, I should make sure the API returns:
Content-Type: application/json
But in my case, the API was misconfigured and returned JSON labeled as text/html
. Since I didn’t have control over the API, I had to find a workaround.
Accept text/html
in the Serializer
Here’s how I adapted the code:
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
serializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
op.responseSerializer = serializer;
By adding text/html
to the list of acceptable content types, I told AFNetworking to try parsing it as JSON anyway and it worked!
My Improve Code
While fixing the error, I realized I could improve the code even further for better learning and real-world use:
- Log raw server response along with parsed JSON.
- Handle status codes more clearly.
- Retry automatically in case of network errors.
Here’s my final version:
NSURL *URL = [NSURL URLWithString:kJSONlink];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
// Allow more content types
AFJSONResponseSerializer *serializer = [AFJSONResponseSerializer serializer];
serializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
operation.responseSerializer = serializer;
__block int retryCount = 0;
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@" JSON Parsed Successfully");
NSLog(@"Parsed JSON: %@", responseObject);
NSString *rawString = [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding];
NSLog(@"Raw Response: %@", rawString);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@" Error: %@", error);
NSInteger statusCode = operation.response.statusCode;
NSLog(@"HTTP Status Code: %ld", (long)statusCode);
// Retry up to 2 times for network errors
if (retryCount < 2 && [error.domain isEqualToString:NSURLErrorDomain]) {
retryCount++;
NSLog(@"🔁 Retrying... (%d)", retryCount);
[[NSOperationQueue mainQueue] addOperation:operation];
}
}];
[[NSOperationQueue mainQueue] addOperation:operation];
Extra Practice Ideas
To deepen my understanding, I practiced with the following scenarios:
- Changed server response headers to simulate
text/html
,text/plain
, andapplication/json
. - Tried
AFXMLParserResponseSerializer
to parse XML instead of JSON. - Set intentional 404 URLs to watch how AFNetworking handled HTML error pages.
- Simulated slow responses with
NSURLProtocol
to learn about timeouts and retries.
These tests helped me understand how AFNetworking actually works under the hood.
Final Thoughts
AFNetworking 2.0 is incredibly powerful but strict when it comes to response formats. This strictness helps prevent bugs but can be confusing when you’re just starting out.
If you’re getting the "unacceptable content-type: text/html"
error:
- First, double-check your endpoint. It might be wrong or redirecting.
- Then, check your server’s content-type headers.
- If needed, expand the
acceptableContentTypes
set but do so carefully.
By adding flexibility and smarter error handling into my code, I turned this debugging frustration into a helpful learning experience. If you’re working with AFNetworking, these small adjustments can save hours of confusion and help you write cleaner, more reliable code.