Ahd Radwan Ahd Radwan - 3 months ago 14
iOS Question

API with text/plain response

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 responseSerializer
with
NSJSONReadingAllowFragments`.

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);
}];

Answer

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];