I have this API call in my application.
For some reason, the response is coming back nil.
If I try the right URL, a JSON will be returned and all the nodes filled out.
So I'm trying to get the error inside the
if let error = error { but even putting a breaking point in there, the code is never reached.
What am I doing wrong ?
Thanks
func getProductById(productId: Int, completion: @escaping (ProductModel) -> ()) {
guard let url = URL(string: "https://mysite/api/products/82") else { return } var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: request) { (data, request, error) in
if let data = data {
print("data") } if let error = error {
print("error") }
guard let data = data else { return } do {
let product = try! JSONDecoder().decode(ProductModel.self, from: data)
DispatchQueue.main.async {
completion(product)
}
} catch {
print(error)
} }
.resume()