Anilkumar iOS Developer
Anilkumar iOS Developer

Reputation: 3745

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.

Upvotes: 7

Views: 11287

Answers (2)

BuLB JoBs
BuLB JoBs

Reputation: 861

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.

Upvotes: 3

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

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

Upvotes: 1

Related Questions