Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 460
TomorrowPlusX Avatar asked Dec 27 '13 21:12

TomorrowPlusX


3 Answers

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"];
like image 159
KIO Avatar answered Oct 20 '22 20:10

KIO


manager.responseSerializer = [AFHTTPResponseSerializer serializer];

work for me.

like image 38
shebelaw Avatar answered Oct 20 '22 19:10

shebelaw


Work for me.

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
like image 9
AaronTKD Avatar answered Oct 20 '22 19:10

AaronTKD