Reputation: 14521
I am trying to get access to the following URL's data in Julia. I can see what appears to be a JSON object when I go to "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&tagged=Julia&site=stackoverflow". However, when I try printing the resulting r
below, it gives me an either text that doesn't render properly, or if I do JSON.print
, it shows me a bunch of random numbers.
How can I use Julia to get the same things I see in the browser (preferably in text form).
r = HTTP.request("GET", "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&tagged=Julia&site=stackoverflow"; verbose=3)
Upvotes: 3
Views: 748
Reputation: 10984
The response body is compressed with gzip
, as you can see from the Content-Encoding
header:
julia> using HTTP
julia> r = HTTP.request("GET", "https://api.stackexchange.com/2.2/questions?order=desc&sort=activity&tagged=Julia&site=stackoverflow")
HTTP.Messages.Response:
"""
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Encoding: gzip <----
[...]
so you have to decompress it with e.g. CodecZlib
:
julia> using CodecZlib
julia> compressed = HTTP.payload(r);
julia> decompressed = transcode(GzipDecompressor, compressed);
From here you can either create a String
(e.g. String(decompressed)
) or parse it with e.g. the JSON
package:
julia> using JSON
julia> json = JSON.parse(IOBuffer(decompressed))
Dict{String,Any} with 4 entries:
"items" => Any[Dict{String,Any}("link"=>"https://stackoverflow.com/questions/59010720/how-to-make-a-request-to-a-specific-url-in-julia","view_count"=>5,"creation_date"=…
"quota_max" => 300
"quota_remaining" => 297
"has_more" => true
(See also https://github.com/JuliaWeb/HTTP.jl/issues/256)
Upvotes: 9