title: How to cancel a URL session request
body: <p>I am upload multiple image to server using convert image to base64 and send image in a API as a parameter. But when we call api again and again then how to stop api calling on button click. I am using below code to call API.</p>
  
  <p>Thanks in advance</p>
  
  <pre><code>let urlPath: String = "URL"
          let url: URL = URL(string: urlPath)!
          var request1 = URLRequest(url: url)
          request1.httpMethod = "POST"
          let stringPost="imgSrc=\(image)"
          let data = stringPost.data(using: String.Encoding.utf8)
          // print("data\(data)")
          request1.httpBody=data
  
          request1.timeoutInterval = 60
          let _:OperationQueue = OperationQueue()
          let task = session.dataTask(with: request1){data, response, err in
              do
              {
                  if data != nil
                  {
                      print("data\(String(describing: data))")
                      if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
                      {
                          DispatchQueue.main.async
                              {
                          print("json\(jsonResult)")
  
  
                      }
                  }
  
              }
              catch let error as NSError
              {
                  DispatchQueue.main.async
                      {
  
                  print("error is \(error)")
                  print("error desc \(error.localizedDescription)")
                  }
              }}
          task.resume()
  </code></pre>