I am trying to call an API, but I need to pass the parameter via querystring. But somehow it's not working. Where is the mistake ?
func getProductById(productId: String, completion: @escaping (Product) -> ()) {
guard let url = URL(string: "https://mysite.com/product/" + productId) 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
guard let data = data else { return }
do {
let product = try! JSONDecoder().decode(Product.self, from: data)
DispatchQueue.main.async {
completion(product)
}
}
catch {
//print(error)
}
}
.resume()
}