1

I am using AFNetworking to get data from erail api(API of Indian railways). I am using this code to get json data

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];

the json data looks like this: enter image description here The following setcompletionblock is returning error.

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"YES!");
        returnedData = [[NSData alloc] initWithData:responseObject];
        done = YES;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"NO!");
        done = YES;
    }];

the error is: po error

Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo=0x100309bb0 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x100238920> { URL: http://api.erail.in/pnr?key=599c66c2-493d-47e6-abe9-af&pnr=673604 } { status code: 200, headers {
"Cache-Control" = private;
"Content-Encoding" = gzip;
"Content-Length" = 239;
"Content-Type" = "text/html; charset=utf-8";
Date = "Wed, 31 Dec 2014 19:32:26 GMT";
Expires = "Thu, 01 Jan 2015 19:32:26 GMT";
Server = "Microsoft-IIS/8.5";
"Set-Cookie" = "ASP.NET_SessionId=fokkb5afytprm3uw1puurljo; path=/; HttpOnly";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://api.erail.in/pnr?key=599c66c2-493d-47e6-abe9-64ae81b&pnr=67360, com.alamofire.serialization.response.error.data=<7b227374 61747573 223a224f 4b222c22 72657375 6c74223a 7b22706e 72223a22 36373336 30343230 3637222c 22636c73 223a2253 4c222c22 65746963 6b657422 3a747275 652c226a 6f75726e 6579223a 2230342d 4a616e2d 32303135 222c2274 7261696e 6e6f223a 22313237 3237222c 226e616d 65223a22 474f4441 56415249 20455850 222c2266 726f6d22 3a224456 44222c22 746f223a 22534322 2c226272 6467223a 22445644 222c2270 61737365 6e676572 73223a5b 7b22626f 6f6b696e 67737461 74757322 3a22572f 4c202032 3332474e 574c222c 22637572 72656e74 73746174 7573223a 2243616e 2f4d6f64 222c2263 6f616368 223a2222 7d5d2c22 63686172 74223a22 43484152 54204e4f 54205052 45504152 4544222c 22657272 6f72223a 22227d7d>, NSLocalizedDescription=Request failed: unacceptable content-type: text/html}

it says the content us html/text but i see json. Any idea how to rectify this?

Morpheus
  • 3,285
  • 4
  • 27
  • 57
  • 1
    Since you don't have control over server's headers you can try this: http://www.sirentuan.com/1514739/codep1/afjsonrequestoperation-failing-because-webservice-returns-json-with-content-type-texthtml You might also contact server administrator to change the content-type header: it should be `application/json` in this case. – Rok Jarc Dec 31 '14 at 20:30
  • I think `[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObjects:@"text/html", nil]];` is deprecated in AFnetworking 2.0 – Morpheus Dec 31 '14 at 20:59
  • 1
    Could be, but there are other similar approaches here: http://stackoverflow.com/a/21119577/653513 for example (uses manager...). Worst case workaround would be to use AFHTTPResponseSerializer and parse the result "manually" – Rok Jarc Dec 31 '14 at 21:06
  • 1
    it is in the docs: http://cocoadocs.org/docsets/AFNetworking/2.5.0/Classes/AFHTTPResponseSerializer.html#//api/name/acceptableContentTypes – Rok Jarc Dec 31 '14 at 21:12

0 Answers0