Alamofire printing "JSON could not be serialized because of error:"

Fatin Wasta picture Fatin Wasta · Dec 1, 2017 · Viewed 8.8k times · Source

I' m trying to make a post request with some params, but while making the request Alamofire prints the following error:

JSON could not be serialized because of error: The data couldn’t be read because it isn’t in the correct format.

Following is my code I'm trying to run:

var parameters: [String: Any] = [:]
parameters[UserParameters.kUserId.rawValue] = self.userInfoForMe.user_id
parameters[UserParameters.kGender.rawValue] = self.userInfoForMe.gender
parameters[UserParameters.kfull_name.rawValue] = self.userInfoForMe.full_name
parameters[UserParameters.kDOB.rawValue] = self.userInfoForMe.dob  

print("parameters:\(parameters)")
let userInfoDataHandler:UserInfoDataHandler = UserInfoDataHandler()
var DataJSON : JSON = JSON.null
userInfoDataHandler.postData(parameters: parameters){
    responseObject, error in
    if responseObject != nil{
        DataJSON = JSON(responseObject!)
       print(DataJSON) 
    }
    else{
        self.removeLoader(showNewController: false)
        self.showAlertWithMessage(msg: ErrorContants.somethingWentWrongError)
    }
}

here is my UserDataInfoHandler:

class UserInfoDataHandler: BaseService {
init(){
    super.init(controller: Controllers.mobile_pa, apiName: ApiNames.save_user_info)
}

}

and following is my code for which gets called from Base Service:

fileprivate func fetchData(method:String, endpoint:String,parameters: [String: Any],completionHandler: @escaping (NSDictionary?, Error?) -> ()){

    //need to show loader here..

    var request: DataRequest? = nil
    let requestURL : String = URLConstants.baseURL + self.controller + "/" + self.apiName
    switch method {
    case RequestMethod.GET.rawValue:
        request = Alamofire.request(requestURL, method: HTTPMethod.get, parameters: parameters)

    case RequestMethod.POST.rawValue:
        request = Alamofire.request(requestURL, method: HTTPMethod.post, parameters: parameters)

    default:
        print(ErrorContants.WentThroughSwitchCaseOf + "Base Service");
    }

    request?.responseJSON(completionHandler: { (response) in
//this block never gets executed as it throws error that JSON is not serialized.
            self.checkForResponseAndError(response: response, completionHandler: completionHandler)
        })
}

After printing my parameters, the following is the output,

parameters:[ "full_name": "fatin", "user_id": "42", "dob": "06-12-1994", "gender": "1"]

Update: I have tried working with other post request and parameters with same code, they are working fine. So I came to know that it's problem with my parameters that I'm sending, but still can't figure out what's going wrong.

Answer

Fatin Wasta picture Fatin Wasta · Dec 2, 2017

After hours of thinking and discussing with my team, figured out there was not any error with my code. The problem was, server side had some validations because of which was getting an PHP error in response, which alamofire can not handle(As alamofire can handle only JSON response). Please find attached screenshot for error in Postman.enter image description here So the actual problem was with response from server which was an PHP Error in post request. Now I'm getting proper response as there is no issue now. Hope someone would find this helpfull.