I trying to retrieve JSON values from URL using below code, I am facing error of reading these values, also I checked the validity of JSON format and it is valid. also, I tried the url using postman and it is working fine.
let params = ["Xvalue":UserDefaults.standard.string(forKey: "com_id"), "Yvalue":UserDefaults.standard.string(forKey: "CID")] as! Dictionary<String, String>
print (params)
var request = URLRequest(url: URL(string: "http://x.x.x/index.php/GetJson")!)
request.httpMethod = "POST"
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { data, response, error -> Void in
do {
let json = try JSONSerialization.jsonObject(with: data!) as! Dictionary<String, AnyObject>
if (json["error"] != nil){
self.hud.dismiss(afterDelay: 3.0)
print(json["error"]!)
}
else if ((json["Code"]?.isEqual("0"))!){
self.hud.dismiss(afterDelay: 3.0)
}else{
self.hud.dismiss(afterDelay: 3.0)
}
} catch {
self.hud.dismiss(afterDelay: 3.0)
print("Error info: \(error)")
}
})
task.resume()
Error description :
Error info: Error Domain=NSCocoaErrorDomain Code=3840 "Garbage at end." UserInfo={NSDebugDescription=Garbage at end.}
JSON Value:
update : I found the issue and explained the answer : https://stackoverflow.com/a/55856132/1484193