Nim HTTP GET JSON with timeout and parse errors
Nim
import std/[httpclient, json] # add -d:ssl for https URLs
proc fetchJson(url: string; timeoutMs = 5_000): JsonNode =
var client = newHttpClient(timeout = timeoutMs)
defer: client.close()
let body = client.getContent(url)
try:
result = parseJson(body)
except JsonParsingError as e:
raise newException(ValueError, "bad JSON from " & url & ": " & e.msg)
let doc = fetchJson("https://example.com/api.json")
echo doc.kind