Search code examples
nim-lang

How to make a HTTP request in Nim


How to make a HTTP request in Nim?


Solution

  • This is answered in the first couple of lines of the httpclient documentation, along with POST, file POST, SSL, Proxy, timeouts and redirect handling...

    Sync:

    import std/httpclient
    
    var client = newHttpClient()
    echo client.getContent("http://google.com")
    

    Async:

    import std/[asyncdispatch, httpclient]
    
    proc asyncProc(): Future[string] {.async.} =
      var client = newAsyncHttpClient()
      return await client.getContent("http://example.com")
    
    echo waitFor asyncProc()