29

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.

Jonathan
  • 614
  • 1
  • 9
  • 18
  • we cant possibly help you without seeing the actual GET request, or at least the value of `_username`. the docs also say that the failure block is executed when the response cannot be parsed, so check the reply as well. – Brad Allred Sep 23 '14 at 03:33
  • ok so now how about `operation.response`? – Brad Allred Sep 23 '14 at 03:37
  • operation.responseSerializationError = error: summary string parsing error – Jonathan Sep 23 '14 at 03:44
  • no, i meant the actual response... http://cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/AFHTTPRequestOperation.html#//api/name/response – Brad Allred Sep 23 '14 at 03:45
  • or whatever way you can get the actual http response head+body. not super familiar with this particular API. – Brad Allred Sep 23 '14 at 03:47
  • NSLog(@"error: %@", operation.responseString) gives--> error: p_large_jldr_475f00025cc22d0e.jpg – Jonathan Sep 23 '14 at 03:51
  • certainly doesnt look like json to me so I would guess that is your problem. in other words, the GET request is successful, but the response cannot be decoded as json (as you have requested by using `AFJSONResponseSerializer`) – Brad Allred Sep 23 '14 at 03:57
  • Apologies. This is the error you were requesting: error: { URL: http://69.91.198.44:8080/GeodatabaseServer/File?username=liubang } { status code: 200, headers { "Content-Length" = 33; Date = "Tue, 23 Sep 2014 03:58:18 GMT"; Server = "Apache-Coyote/1.1"; } } – Jonathan Sep 23 '14 at 03:58

3 Answers3

39

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.

Brad Allred
  • 7,323
  • 1
  • 30
  • 49
  • Thank you for your help Brad. I am new to JSON. If I upload a file as JSON to the Apache server, when I do a request to GET should it not be a valid JSON, or could it be altered by the server? – Jonathan Sep 23 '14 at 04:17
  • @Jonathan I'm not sure what you are asking. you cannot upload anything via GET. the error you are getting is due to the server *response* to your GET request, not your GET request itself. GET requests are not typically json either. responses for a server can be anything at all. – Brad Allred Sep 23 '14 at 04:27
  • No I meant I had uploaded using the POST method where I used JSON. So if I upload using JSON I can download with a different serializer? – Jonathan Sep 23 '14 at 04:40
  • @Jonathan the `responseSerializer` should be selected based on the type of *response* data you expect from the server. it has nothing to do with what you are posting. – Brad Allred Sep 23 '14 at 14:33
  • I switched it to the AFHTTPResponseSerializer and appears to be working. I just need to test it when I get a chance. Thanks Brad. – Jonathan Sep 23 '14 at 15:00
  • Hello,@Brad Allred , but i am not using AFHTTPResponseSerializer for Calling WebService , i am using defualt NSJsonSerialization . then what is the change in that method for this particular error?..... Please Help me ASAP. – Hiren kanetiya Apr 21 '17 at 13:21
  • @Hirenkanetiya that is the problem, you _can't_ use `NSJsonSerialization` for payloads that are _not_ JSON. its that simple. Use `AFJSONResponseSerializer` or something more appropriate instead. – Brad Allred Apr 21 '17 at 16:09
  • I meant `AFHTTPResponseSerializer` – Brad Allred Apr 21 '17 at 16:18
3

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.

Santi Pérez
  • 370
  • 4
  • 12
-2

Hey guys this is what I found to be my issue: I was calling Alamofire via a function to Authenticate Users: I used the function "Login User" With the parameters that would be called from the "body"(email: String, password: String) That would be passed

my errr was exactly:

optional(alamofire.aferror.responseserializationfailed(alamofire.aferror.responseserializationfailurereason.jsonserializationfailed(error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0

character 0 is the key here: meaning the the call for the "email" was not matching the parameters: See the code below

func loginUser(email: String, password: String, completed: @escaping downloadComplete) { let lowerCasedEmail = email.lowercased()

    let header = [
        "Content-Type" : "application/json; charset=utf-8"
    ]
    let body: [String: Any] = [
        "email": lowerCasedEmail,
        "password": password
    ]

    Alamofire.request(LOGIN_USER, method: .post, parameters: body, encoding: JSONEncoding.default, headers: header).responseJSON { (response) in
        if response.result.error == nil {

            if let data = response.result.value as? Dictionary<String, AnyObject> {
                if let email = data["user"] as? String {
                    self.userEmail = email
                    print(self.userEmail)
                }
                if let token = data["token"] as? String {
                    self.token_Key = token
                    print(self.token_Key)
                }

"email" in function parameters must match the let "email" when parsing then it will work..I no longer got the error...And character 0 was the "email" in the "body" parameter for the Alamofire request:

Hope this helps

berkat0789
  • 51
  • 3