0

I am trying to call login API with Alamofire and I am getting the response when I am using reponseString but throwing error with responseJSON and the error says: failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 2." UserInfo={NSDebugDescription=Invalid value around character 2.})))

And when I hiting the api from postman it's showing me the response as:

{
    "status": true,
    "user_id": "39",
    "message": "Record Found",
    "data": {
        "id": "39",
        "username": "user25",
        "email": "xyz@gmail.com",
        "invite_code": "zZ3sq1jM",
        "facebook_id": null,
        "google_id": null,
        "first": "Abhay",
        "last": "Singh",
        "bday": "2021-05-19 00:00:00",
        "city": "Punjab",
        "state": "Delhi",
        "zip": null,
        "address": null,
        "gender": "male",
        "photo": "",
        "bio": "Hi",
        "weight": "12.00",
        "is_coach": false
    }
}

my network API calling method is:

class func postWebServiceCall(_ strURL : String, params : [String : Any]?, isShowLoader : Bool, success : @escaping SuccessHandler,  failure :@escaping FailureHandler){
        isShowLoader  == true ? CommonUtils.showHud() : nil
        print("postWebServiceCall with params => \(params ?? [:]) ")
        if isConnectedToNetwork()  {
            //var headers = HTTPHeaders()
            //headers = ["content-type" : "application/json"]
            
            AF.request(strURL, method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).responseJSON(completionHandler: {(resObj) -> Void in
                CommonUtils.hideHud()
                let responseCode = resObj.response?.statusCode
                //let responseDict = resObj as? [String : Any] ?? [:]
                if let safeResponeCode = responseCode {
                    switch safeResponeCode {
                    case 200:
                        switch resObj.result {
                            case .success(let value):
                             let res = JSON(value)
                             success(res)
                            case .failure(let error):
                                failure(error)
                                CommonUtils.showToastMessage(message: "Something went wrong.")
                        }
                    default:
                        print("something went wrong")
                        CommonUtils.hideHud()
                        CommonUtils.showToastMessage(message: "Something went wrong.")
                    }
                }
            })
        } else {
            CommonUtils.hideHud()
            CommonUtils.showToastMessage(message: "No internet connection.")
            
        }
    }

0 Answers0