Ahd Radwan
Ahd Radwan

Reputation: 1100

API with text/plain response failed with 'unacceptable content-type'

I'm trying to perform a GET request that responses with 'text/plain'

The request failed with error "unacceptable content-type: text/plain".

so I set the AcceptableContentType to "text/plain" and created a responseSerializerwithNSJSONReadingAllowFragments`.

But the request Also failed with error .

NSCocoaErrorDomain" - code: 3840" : "Invalid value around character 0."

      NSString *requestUrl = [NSString
                          stringWithFormat:@"%@%@",kServerURL,myURL];

  AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

  manager.requestSerializer = [AFJSONRequestSerializer serializer];
  manager.responseSerializer = [AFJSONResponseSerializer
      serializerWithReadingOptions:NSJSONReadingAllowFragments];

  [manager.responseSerializer
      setAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
  [manager GET:requestUrl
      parameters:nil
      progress:nil
      success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
      }
      failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
      }];

Upvotes: 0

Views: 527

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

in this place

manager.requestSerializer = [AFJSONRequestSerializer serializer];
 manager.responseSerializer = [AFJSONResponseSerializer
  serializerWithReadingOptions:NSJSONReadingAllowFragments];
[manager.responseSerializer
  setAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];

use

    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];

Upvotes: 2

Related Questions