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

3

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.

Encompass answered 23/9, 2014 at 2:54 Comment(8)
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.Assist
ok so now how about operation.response?Assist
operation.responseSerializationError = error: summary string parsing errorEncompass
no, i meant the actual response... cocoadocs.org/docsets/AFNetworking/1.3.1/Classes/…Assist
or whatever way you can get the actual http response head+body. not super familiar with this particular API.Assist
NSLog(@"error: %@", operation.responseString) gives--> error: p_large_jldr_475f00025cc22d0e.jpgEncompass
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)Assist
Apologies. This is the error you were requesting: error: <NSHTTPURLResponse: 0x16588b40> { URL: 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"; } }Encompass
A
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.

Assist answered 23/9, 2014 at 4:6 Comment(8)
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?Encompass
@Encompass 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.Assist
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?Encompass
@Encompass 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.Assist
I switched it to the AFHTTPResponseSerializer and appears to be working. I just need to test it when I get a chance. Thanks Brad.Encompass
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.Obligee
@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.Assist
I meant AFHTTPResponseSerializerAssist
T
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.

Tedi answered 27/7, 2016 at 15:43 Comment(0)
M
-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

Mccully answered 14/11, 2017 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.