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.
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"];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
work for me.
Work for me.
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
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;
Just use:
SWIFT:
manager.responseSerializer.acceptableContentTypes = NSSet(object: "text/plain") as Set<NSObject>
OBJ-C:
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
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
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>