AFNetworking 2.0 - "unacceptable content-type: text/plain"

TomorrowPlusX picture TomorrowPlusX · Dec 27, 2013 · Viewed 38.3k times · Source

I'm using AFNetworking 2.0 to read JSON from a service I'm building (on localhost for now) in Node. Pretty normal stuff.

Node is sending JSON like so:

res.setHeader('Content-Type','application/json');
res.end( JSON.stringify(...));

My iOS first-pass code is attempting to read that data like so:

typedef void(^NextBlock)();
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager GET:self.newestTimestampURL.absoluteString
    parameters:nil
    success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        //NSDictionary *response =
        NSLog(@"got %@", responseObject );
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error)
    {
        NSLog(@"fail %@", error );
    }];

This is the error I'm getting:

Error Domain=AFNetworkingErrorDomain Code=-1016 "Request failed: unacceptable content-type: text/plain" UserInfo=0xb783e30 {NSErrorFailingURLKey=http://localhost:3000/api/v1/log/newest, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0xb656a70> { URL: http://localhost:3000/api/v1/log/newest } { status code: 200, headers {
Connection = "keep-alive";
ContentType = "application/json";
Date = "Fri, 27 Dec 2013 20:58:50 GMT";
"Transfer-Encoding" = Identity;
} }, NSLocalizedDescription=Request failed: unacceptable content-type: text/plain}

I can curl (-i) the url http://localhost:3000/api/v1/log/newest and get the data I'm expecting, and it's application/json as expected. If I load that data in my web browser, I get JSON as expected per dev tools inspector.

But using AFNetworking, I get this mysterious "unacceptable content-type: text/plain" error. Any idea?

NOTE: I've never used AFNetworking before, so I'm probably using it incorrectly.

Answer

KIO picture KIO · Jan 14, 2014

It seems that the server is sending "text/html", and this type is not supported by default. Add @"text/html" for "acceptableContentTypes"

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