I get this error:
    
    
    Error: an error happened while downloading a file!
    main.nim(72)             main
    main.nim(66)             downloadFile
    httpclient.nim(1305)     downloadFile
    httpclient.nim(1235)     get
    httpclient.nim(1227)     request
    httpclient.nim(1204)     request
    httpclient.nim(1173)     requestAux
    httpclient.nim(1109)     newConnection
    net.nim(1566)            dial
    nativesockets.nim(262)   getAddrInfo
    oserr.nim(66)            raiseOSError
    
    main.nim(72)             main
    main.nim(66)             downloadFile
    httpclient.nim(1305)     downloadFile
    httpclient.nim(1235)     get
    httpclient.nim(1227)     request
    httpclient.nim(1204)     request
    httpclient.nim(1173)     requestAux
    httpclient.nim(1109)     newConnection
    net.nim(1566)            dial
    nativesockets.nim(262)   getAddrInfo
    oserr.nim(66)            raiseOSError
    [[reraised from:
    main.nim(72)             main
    main.nim(70)             downloadFile
    ]]
    Error: unhandled exception: No such file or directory; Additional info: 
"Name or service not known" [OSError]
    Error: execution of an external program failed: ''main '
    
    
    
    Run

I copied things from choosenim:
    
    
    import os, strutils, httpclient
    from uri import parseUri
    from terminal import eraseLine
    from sequtils import map
    
    proc getProxy*(): Proxy =
      ## Returns ``nil`` if no proxy is specified.
      var url = ""
      try:
        if existsEnv("http_proxy"):
          url = getEnv("http_proxy")
        elif existsEnv("https_proxy"):
          url = getEnv("https_proxy")
      except ValueError:
        echo("Warning:", "Unable to parse proxy from environment: " & 
getCurrentExceptionMsg())
      if url.len > 0:
        var parsed = parseUri(url)
        if parsed.scheme.len == 0 or parsed.hostname.len == 0:
          parsed = parseUri("http://"; & url)
        let auth =
          if parsed.username.len > 0: parsed.username & ":" & parsed.password
          else: ""
        return newProxy($parsed, auth)
      else:
        return nil
    
    const progressBarLength = 50
    
    proc showIndeterminateBar(progress, speed: BiggestInt, lastPos: var int) =
      eraseLine()
      if lastPos >= progressBarLength:
        lastPos = 0
      var spaces = repeat(' ', progressBarLength)
      spaces[lastPos] = '#'
      lastPos.inc()
      stdout.write("[$1] $2mb $3kb/s" % [
                      spaces, $(progress div (1000*1000)),
                      $(speed div 1000)
                    ])
      stdout.flushFile()
    
    proc showBar(fraction: float, speed: BiggestInt) =
      try:
        eraseLine()
      except OSError:
        discard
      let hashes = repeat('#', int(fraction * progressBarLength))
      let spaces = repeat(' ', progressBarLength - hashes.len)
      stdout.write("[$1$2] $3% $4kb/s" % [
                      hashes, spaces, formatFloat(fraction * 100, precision=4),
                      $(speed div 1000)
                    ])
      stdout.flushFile()
    
    proc downloadFile*(url, outputPath: string) =
      try:
        var client = newHttpClient(proxy = getProxy())
        var lastProgressPos = 0
        proc onProgressChanged(total, progress, speed: BiggestInt) {.closure, 
gcsafe.} =
          let fraction = progress.float / total.float
          if fraction == Inf:
            showIndeterminateBar(progress, speed, lastProgressPos)
          else:
            showBar(fraction, speed)
        client.onProgressChanged = onProgressChanged
        client.downloadFile(url, outputPath)
      except Exception as ex:
        echo("Error: an error happened while downloading a file!")
        echo(getStackTrace(ex))
        raise ex
    
    downloadFile("https://nim-lang.org/docs/httpclient.html";, "httpclient.html")
    
    
    Run

Reply via email to