Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 593
Jonathan Avatar asked Sep 23 '14 02:09

Jonathan


2 Answers

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.

like image 146
Brad Allred Avatar answered Nov 20 '22 16:11

Brad Allred


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.

like image 3
Santi Pérez Avatar answered Nov 20 '22 15:11

Santi Pérez