(Cocoa error 3840.)" (Invalid value around character 0.) AFNetworking

43,645

Solution 1

Judging by the discussion in the comments it appears that your GET request is successful (response code 200), but the response body is not valid JSON (nor a JSON fragment) as you have requested by your use of AFJSONResponseSerializer. A basic AFHTTPResponseSerializer can be used for responses that are not JSON.

Solution 2

I am pretty sure that you have a valid response from server but your response is not a valid format in JSON, probably because you have carachters in front of first { . Please try this: Put the same URL address manually in your browser and you will see the culprit in the response. Hope it helped.

Share:
43,645
Jonathan
Author by

Jonathan

Updated on November 15, 2020

Comments

  • Jonathan
    Jonathan over 3 years

    I've been getting the following error when using the GET method to retrieve a file from a server:

    Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x16e81ed0 {NSDebugDescription=Invalid value around character 0.}
    

    I've tried a number of different things and I believe it could be something to do with the JSON format on the file that I'm trying to get.

    Here is the code I've been using:

    _username = @"JonDoe";
    NSDictionary *parameters = @{ @"username" : _username};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    [manager GET:@"http://.........."
      parameters:parameters
         success:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"JSON: %@", responseObject);
         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
             NSLog(@"Error: %@", error);
         }];
    

    My POST method works fine. I just can't seem to fix this issue with the GET. Any ideas? Thank you.