28

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.

TomorrowPlusX
  • 1,205
  • 2
  • 14
  • 27
  • If you `curl` with the `-I` flag to just get the headers. Has the response got the correct content type header at this point? – Paul.s Dec 27 '13 at 21:19
  • When I curl -I I get the following: `HTTP/1.1 200 OK ContentType: application/json Date: Sat, 28 Dec 2013 14:26:04 GMT Connection: keep-alive` – TomorrowPlusX Dec 28 '13 at 14:26
  • Shouldn't it be `Content-Type` with a hyphen? – Paul.s Dec 28 '13 at 15:24
  • Oh, holy hell. Thanks! I had assumed my client code was at fault, but it was a typo in the node JS code. Thanks so much! A christmas miracle! – TomorrowPlusX Dec 28 '13 at 15:53

7 Answers7

40

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"];
Igor Kovryzhkin
  • 2,195
  • 1
  • 27
  • 22
22

manager.responseSerializer = [AFHTTPResponseSerializer serializer];

work for me.

shebelaw
  • 3,992
  • 6
  • 35
  • 48
  • works for me as well, I have used `manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];` but it did not help me. But your solution works. – Matrosov Oleksandr Feb 01 '15 at 16:31
9

Work for me.

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
AaronTKD
  • 109
  • 1
  • 3
  • This works but is a poor solution because it does not add "text/plain" to the list of acceptable types, but replaces it instead, meaning that other valid types - like "application/json" will no longer work. The more correct (and complete) answer is provided by ABS. – Airsource Ltd Sep 12 '16 at 09:50
4

Just use:

SWIFT:

manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>

OBJ-C:

manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
David Zorychta
  • 13,039
  • 6
  • 45
  • 81
rsc
  • 10,348
  • 5
  • 39
  • 36
  • http://stackoverflow.com/questions/42573527/calling-web-services-with-the-help-of-afnetworking-in-objective-c/42573800#42573800 – Harshit Goel Mar 03 '17 at 09:49
4

I have followed the answer posted by @KIO but it did not work in my scenario.

After going with many answers i was able to find solution which worked for me.

AFHTTPRequestOperationManager *operation = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

AFJSONResponseSerializer *jsonResponseSerializer = [AFJSONResponseSerializer serializer];

NSMutableSet *jsonAcceptableContentTypes = [NSMutableSet setWithSet:jsonResponseSerializer.acceptableContentTypes];
[jsonAcceptableContentTypes addObject:@"text/plain"];
jsonResponseSerializer.acceptableContentTypes = jsonAcceptableContentTypes;
operation.responseSerializer = jsonResponseSerializer;
ABS
  • 7,634
  • 1
  • 21
  • 20
  • This is the only one that worked for me. It didn't break the existing json types and can be easily adapted to include even more types. Thanks. – Nublodeveloper Dec 26 '17 at 10:38
0
NSURL *url = [NSURL URLWithString:RSS_WORLD_NEWS];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

// Make sure to set the responseSerializer correctly
operation.responseSerializer = [AFXMLParserResponseSerializer serializer];

operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/rss+xml"];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSXMLParser *XMLParser = (NSXMLParser *)responseObject;
    [XMLParser setShouldProcessNamespaces:YES];

     XMLParser.delegate = self;
     [XMLParser parse];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving contents"
                                                        message:[error localizedDescription]
                                                       delegate:nil
                                              cancelButtonTitle:@"Ok"
                                              otherButtonTitles:nil];
    [alertView show];

}];

[operation start];

work for me

0

Updated Solution for Swift.

If you are using AFNetworking in Swift. Then this solution might help you. It will accept most of the content types.

let manager=AFHTTPRequestOperationManager()

            manager.responseSerializer = AFJSONResponseSerializer(readingOptions: NSJSONReadingOptions.AllowFragments) as AFJSONResponseSerializer

            manager.requestSerializer = AFJSONRequestSerializer() as AFJSONRequestSerializer

            manager.responseSerializer.acceptableContentTypes = NSSet(objects:"application/json", "text/html", "text/plain", "text/json", "text/javascript", "audio/wav") as Set<NSObject>
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78