2

I've a trouble with requests using AFNetworiking 2.

I have my AFHTTPSessionManager custom sharedInstance in my sharedInstance class method

sharedInstance.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:3];
sharedInstance.responseSerializer.acceptableContentTypes = [sharedInstance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
sharedInstance.responseSerializer.acceptableContentTypes = [sharedInstance.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

This is because my server responds with text/plain (only on errors - on success it responses with jsons)

But even that when I send a POST method, I get this error:

failure: error: Error Domain=NSCocoaErrorDomain Code=3840 
"The operation couldn’t be completed. (Cocoa error 3840.)" 
(JSON text did not start with array or object and option to allow fragments not set.)
UserInfo=0x175e6970 {NSDebugDescription=JSON text did not start with array or object
and option to allow fragments not set., NSUnderlyingError=0x175e59b0 "Request failed:
internal server error (500)"}

I've read questions: this and this and some more stuff in the web but there is no good answer. Any ideas?

Community
  • 1
  • 1
zioolek
  • 421
  • 5
  • 15

6 Answers6

2

Try something like this:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:url]];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
Andrew McKinley
  • 1,137
  • 1
  • 6
  • 11
2

use swift try this

manager.responseSerializer.acceptableContentTypes = nil
Beslan Tularov
  • 3,111
  • 1
  • 21
  • 34
1

Internal server error (500) basically means your server is not able to successfully handle this request, but goes in error. (You may want to expose here portion of server code to get more help). From your question, I understand that in case of error, the server answers in text/plain. I suppose that your code raises this error (JSON text did not start with array...) because you are trying to parse the response as JSON (expected when no error occurs) even in the case of a 500 error.

You may want to share the rest of your client code. Correcting the server side issue would help, but would not lead to a proper client code logic for the error cases.

If you need to access Http error code, have a look here : is there an easy way to get the http status code in the failure block from AFHTTPClient?

Possible similar to your issue here : AFNetworking 2.0: NSLocalizedDescription=Request failed: unacceptable content-type: text/html

Community
  • 1
  • 1
blackbox
  • 671
  • 6
  • 18
1

For OC, I solved the problem by this way

AFHTTPSessionManager* manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = nil;

Hope this will help.

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
Cam Chu
  • 71
  • 1
  • 3
0

Just went through something similar yesterday!

Here's what I did.

Go to your afnetworking class, then AFURLResponseSerializer.m .

Find this method, then make sure it has NSJSONReadingAllowFragments.

+ (instancetype)serializer {
    return [self serializerWithReadingOptions:(NSJSONReadingOptions)NSJSONReadingAllowFragments];
}

Try running it again, you may get another error, Invalid value around character 0.

So yeah, your server is bull, it's giving garbage json.

You can check it though!

So in your failure possibility put:

NSLog("%@", operation.responseString);

Most servers say in the reply whether it was a success or not, mine does anyway.

So search the responseString for Success like below.

(in failure:)

 if ([operation.responseString containsString:@"SUCCESS"]) {                 
//true success
} else {              
NSLog(@"Error: %@", error);
//true failure
}
0

Surprisingly this question does not have a complete answer, while @blackbox is very close.

First of all, as @blackbox said, as server responses with 500 Internal Server Error, probably displayed as text/html or text/plain by default, it's not accepted by AFNetworking 2's AFJSONResponseSerializer; it accepts MIME type equals to JSON only (e.g. application/json).

Therefore, to tackle this, in failure block, you can detect such error and relevant display error message.

Raptor
  • 53,206
  • 45
  • 230
  • 366