Home > Blockchain >  Questions about go-curl's OPT_COOKIEJAR option
Questions about go-curl's OPT_COOKIEJAR option

Time:11-02

When using the OPT_COOKIEJAR option in go-curl, is there a way to check whether the work is completed?

If you run the source code below, there is a problem that ReadFile is executed before the cookie download is completed. I want to solve this.

  CommonSetopt(easy)
  easy.Setopt(curl.OPT_VERBOSE, 0)
  easy.Setopt(curl.OPT_COOKIEJAR, cookieName)
  easy.Setopt(curl.OPT_URL, string("https://drive.google.com/uc?export=download&id=" gdriveID))

  log.Println("start download cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie download fail: ", err)
      return
  }

  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie read fail: ", err)
      return
  }

CodePudding user response:

You need to cleanup the session immediately after easy.Perform instead of cleaning up using a defer statement

  CommonSetopt(easy)
  easy.Setopt(curl.OPT_VERBOSE, 0)
  easy.Setopt(curl.OPT_COOKIEJAR, cookieName)
  easy.Setopt(curl.OPT_URL, string("https://drive.google.com/uc?export=download&id=" gdriveID))

  log.Println("start download cookie: ", url)
  if err := easy.Perform(); err != nil {
      log.Println("cookie download fail: ", err)
      easy.Cleanup()
      return
  }
  // do cleanup
  easy.Cleanup()
  readBuf, err := ioutil.ReadFile(cookieName)
  if err != nil {
      log.Println("cookie read fail: ", err)
      return
  }
  • Related