Julia HTTP GET JSON with connect timeout
Julia
using Downloads, JSON3
function get_json(url; timeout=10.0)
buf = IOBuffer()
resp = Downloads.request(url; method="GET", output=buf, timeout=timeout)
resp.status in 200:299 || error("HTTP $(resp.status) for $url")
body = String(take!(buf))
isempty(strip(body)) && return nothing
try
return JSON3.read(body)
catch err
throw(ArgumentError("invalid JSON from $url: $(sprint(showerror, err))"))
end
end
# get_json("https://api.example.com/items"; timeout=5.0)