When calling the Stack Exchange API v2.2 using the new (Mma11) function URLRead
we get a GZIP encoded Body in alternative formats: "Body"
, "BodyByteArray"
and "BodyBytes"
depending on the options.
How can we deal with each of these formats in order to decode the body in Mathematica?
I am particularly intrigued with operating on ByteArray
I know I could get the content using
Import[URLBuild[{"https://api.stackexchange.com", "2.2",
"info"}, {"site" -> "mathematica"}], "RawJSON"]
My questions is about dealing explicitly with list of byte values as well as ByteArray
that are encoded. Hopefully without creating a temporary file, to then read it back.
The code I'm using:
reply=URLRead[
URLBuild[{"https://api.stackexchange.com", "2.2", "info"}, {"site" -> "mathematica"}]
, {"Headers", "StatusCode", "StatusCodeDescription", "ContentType",
"BodyByteArray"}]
<|"Headers" -> { "cache-control" -> "private" , "content-type" -> "application/json; charset=utf-8" , "content-encoding" -> "gzip" , "access-control-allow-origin" -> "*" , "access-control-allow-methods" -> "GET, POST" , "access-control-allow-credentials" -> "false" , "x-content-type-options" -> "nosniff" , "date" -> "Thu, 11 May 2017 10:55:51 GMT" , "content-length" -> "234" } , "StatusCode" -> 200 , "StatusCodeDescription" -> "OK" , "ContentType" -> "application/json; charset=utf-8" , "BodyBytesArray" -> ByteArray[< 234 >] |>
By the way, strangely to me, Import[URLRead[url], "RawJSON"]
doesn't work.