My code:
import std/[asyncdispatch, httpclient, json]
proc asyncProc(): Future[string] {.async.} =
var client = newAsyncHttpClient()
try:
let response = await client.getContent("https://randomuser.me/api/")
result = parseJson(response).getStr()
finally:
client.close()
proc main() {.async.} =
let results = await asyncProc()
echo results
waitFor main()
Compiling this using:
nim c -r --verbosity:0 -d:ssl fetch.nim
Nothing is echoed out in the terminal, what exactly am I doing wrong here?
JsonNode
is an object variant. It provides getters such as getStr()
to either return the value that the given JsonNode
holds or the default value for the expected type.
In your example the following holds true:
let result = parseJson(response)
assert result.kind == JObject
assert result.getStr() == ""
You can convert the JsonNode
to a string using the $
operator: $result
.
For example:
let response = await client.getContent("https://randomuser.me/api/")
result = $parseJson(response)