Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0

16,701

The Error saying that the response from server is not a valid JSON string. Can you try responseString instead of responseJSON like

Alamofire.request(urlAddress, method: requestMethod, parameters: params).responseString{ response in
    debugPrint(response)
}

See the Xcode debugger output & change it according to your need.

Share:
16,701
Egle Matutyte
Author by

Egle Matutyte

Updated on June 04, 2022

Comments

  • Egle Matutyte
    Egle Matutyte almost 2 years

    Hi I am new at swift and I got problem, I made a request to server with post method and I get response with good Json, after that I am makeing another request with get method but I get this error.

    Error: responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})

    Parameters for request:

    static func getInformationFromConfig(token: String, config: String, section : String, option: String) -> [String:Any] {
    
            let getInformationFromConfigparam: [String : Any] = ["jsonrpc": "2.0",
                                          "id": 1,
                                          "method": "call",
                                          "params": [ token, "uci", "get", [ "config": config, "section": section, "option": option]]
        ]
    
        return getInformationFromConfigparam
    }
    
    
    
     public func device(token: String, loginCompletion: @escaping (Any) -> ()) {
        let deviceinfo = JsonRequests.getInformationFromConfig(token: token, config: "wireless", section: "@wifi-iface[0]", option: "mode")
        makeWebServiceCall(urlAddress: URL, requestMethod: .get, params: deviceinfo, completion: { (JSON : Any) in
            loginCompletion(JSON)
        })
    }
    

    Request:

    private func makeWebServiceCall (urlAddress: String, requestMethod: HTTPMethod, params:[String:Any], completion: @escaping (_ JSON : Any) -> ()) {
    
    
        Alamofire.request(urlAddress, method: requestMethod, parameters: params, encoding: JSONEncoding.default).responseJSON{ response in
    
    
            switch response.result {
            case .success(let value):
    
                let json = JSON(value)
    
                if let jsonData = response.result.value {
    
                    completion(jsonData)
                }
    
    
            case .failure(let error):
    
                    completion("Failure Response: \(error)")
    

    ResponseString response:

     [Request]: GET http://192.168.1.1/ubus
    [Response]: <NSHTTPURLResponse: 0x60000003c4a0> { URL: http://192.168.1.1/ubus } { status code: 400, headers {
    Connection = "Keep-Alive";
    "Content-Type" = "text/html";
    "Keep-Alive" = "timeout=20";
    "Transfer-Encoding" = Identity;
    } }
    [Data]: 35 bytes
        [Result]: FAILURE:     responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}))