Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840

I am working on swift project and calling webservice with Alamofire. But, while calling post method, I am getting following error.

Header file :

let accessTokenHeaderFile = [
    "Accept": "application/json",
    "Content-Type" :"application/json",
    "X-TOKEN" : UtilityClass.sharedInstance.accessTokenString
]

        Alamofire.request(urlString, method: .post, parameters: params as? [String:Any], encoding: JSONEncoding.default, headers: accessTokenHeaderFile).responseJSON { response in
          requestVC.removeLoader()
            switch (response.result) {
            case .success:
                if response.result.value != nil{
                    completionHandler (response.result.value)
                }
                break
            case .failure(let error):
                failureHandler (error as NSError?)
                break
            }
        }

And the error is

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

Can anyone suggest me, how to fix this, I tried googling, but whatever I found the answers not helped me.

like image 761
Anilkumar iOS - ReactNative Avatar asked Oct 27 '17 08:10

Anilkumar iOS - ReactNative


2 Answers

Error of 3840 saying that the response from server is not a valid JSON string. So you can check you parameters key value may be it’s wrong assign because similar of responseString instead of responseJSON.

like image 77
BuLB JoBs Avatar answered Oct 07 '22 08:10

BuLB JoBs


Your response is not a valid json hence you're getting this error. Please check the response.response?.statusCode to see what is server returning. And if you want to see the actual response try using responseString or responseData methods instead of responseJSON

e.g.

Alamofire.request(urlString, method: .post, parameters: params as? [String:Any], encoding: JSONEncoding.default, headers: accessTokenHeaderFile). responseData {

You can find out more response methods here

like image 1
Inder Kumar Rathore Avatar answered Oct 07 '22 09:10

Inder Kumar Rathore