Http Request with Querystring Parameter

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()
      
     
  }
Answered by ForumsContributor in
Accepted Answer

There is no "major" mistake in your code. However, with querying parameters in http, you have to use "?" to indicate this is a query string. If you have more than one parameter you need to separate them with "&". Something like:

URL(string: "https://mysite.com/product?id=" + productId)

You need to find out exactly how to query the server with productId.

Http Request with Querystring Parameter
 
 
Q