-4

Here is my code

        Alamofire.request(URL, method: requestType, parameters: param, encoding: URLEncoding.default, headers: headers).responseJSON { (response:DataResponse<Any>) in


        switch(response.result) {
        case .success(_):
            if response.result.value != nil{


                completion(response.result.value!)
            }
            break

        case .failure(_):
            print(response.result.error!)
            failure(response.result.error!)
            break

        }
    }

responseSerializationFailed(Alamofire.AFError.ResponseSerializationFailureReason.jsonSerializationFailed(Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.})) Could not cast value of type 'Alamofire.AFError' (0x100969000) to 'Swift.Array<__ObjC.NSDictionary>' (0x174094340). 2017-11-29 16:01:09.744776 WTV_GO[2333:1690232] Could not cast value of type 'Alamofire.AFError' (0x100969000) to 'Swift.Array<__ObjC.NSDictionary>' (0x174094340).

I got this error when putting firebase token and other parameters to the server but same parameters works fine through Postman, is there any solution? Thank you!

2 Answers2

1

Finally, solved this problem. I have changed code to encoding: URLEncoding.queryString from encoding: URLEncoding.default

0

As per your error looks like you have not handle error. Please look at follow sample code to handle success response result and error response result.

    Alamofire.request(url, method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: nil) .responseJSON { response in
                    switch response.result {
                    case .success(_):
                        print(response)
                        // Do your stuff here
                    case .failure(_):

                        print("Request failed with error: \(response.result.error ?? "" as! Error)")
                        //handle error here
                    }
Shahrukh
  • 740
  • 7
  • 25