3 duplicates list edited from Importing a Base64 encoded string to Importing a Base64 encoded string, Decoding GZIP encoded Body, BodyBytes (ByteArray) and BodyBytesArray from URLRead
| link
    Post Closed as "duplicate" by Kuba of
2 added 783 characters in body; edited title
source | link

GZIP decode fails on stringwhen done manually

So I hit a minor snag in testing the users part of an API connection to SE. It seems Mathematica can't always decode the GZIP?

For instance this:

ImportString[
 FromCharacterCode[
  URLFetch["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
   "ContentData"
   ]
  ],
 "GZIP"
 ]

Throws:

Import::infer: Cannot infer format of file InputStream[Name: String, Unique ID: 3].

On the other hand this:

Import["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
 "Text"
 ]

Seems to work totally fine.

How can I import the string the same way Import does?

Update:

Another wrinkle. If I import the bytes:

cdata = URLFetch[
   "https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
   "ContentData"
   ];

Then dump them to a file:

$setempfile = CreateFile[];
BinaryWrite[$setempfile, cdata];

I can import that file as bytes, but it's different from what I wrote:

In[196]:= cdata == Import[$setempfile, "Byte"]

Out[196]= False

And what is now read-in can be decoded properly:

In[197]:= StringTake[FromCharacterCode@Import[$setempfile, "Byte"], 50]

Out[197]= "{\"items\":[{\"badge_counts\":{\"bronze\":21,\"silver\":14"

So I'm doubly confused, as

Import[$setempfile, "gzip"]

Still fails.

GZIP decode fails on string

So I hit a minor snag in testing the users part of an API connection to SE. It seems Mathematica can't always decode the GZIP?

For instance this:

ImportString[
 FromCharacterCode[
  URLFetch["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
   "ContentData"
   ]
  ],
 "GZIP"
 ]

Throws:

Import::infer: Cannot infer format of file InputStream[Name: String, Unique ID: 3].

On the other hand this:

Import["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
 "Text"
 ]

Seems to work totally fine.

How can I import the string the same way Import does?

GZIP decode fails when done manually

So I hit a minor snag in testing the users part of an API connection to SE. It seems Mathematica can't always decode the GZIP?

For instance this:

ImportString[
 FromCharacterCode[
  URLFetch["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
   "ContentData"
   ]
  ],
 "GZIP"
 ]

Throws:

Import::infer: Cannot infer format of file InputStream[Name: String, Unique ID: 3].

On the other hand this:

Import["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
 "Text"
 ]

Seems to work totally fine.

How can I import the string the same way Import does?

Update:

Another wrinkle. If I import the bytes:

cdata = URLFetch[
   "https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
   "ContentData"
   ];

Then dump them to a file:

$setempfile = CreateFile[];
BinaryWrite[$setempfile, cdata];

I can import that file as bytes, but it's different from what I wrote:

In[196]:= cdata == Import[$setempfile, "Byte"]

Out[196]= False

And what is now read-in can be decoded properly:

In[197]:= StringTake[FromCharacterCode@Import[$setempfile, "Byte"], 50]

Out[197]= "{\"items\":[{\"badge_counts\":{\"bronze\":21,\"silver\":14"

So I'm doubly confused, as

Import[$setempfile, "gzip"]

Still fails.

1
source | link

GZIP decode fails on string

So I hit a minor snag in testing the users part of an API connection to SE. It seems Mathematica can't always decode the GZIP?

For instance this:

ImportString[
 FromCharacterCode[
  URLFetch["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
   "ContentData"
   ]
  ],
 "GZIP"
 ]

Throws:

Import::infer: Cannot infer format of file InputStream[Name: String, Unique ID: 3].

On the other hand this:

Import["https://api.stackexchange.com/2.2/users?site=mathematica.\
stackexchange.com&pagesize=100&page=5",
 "Text"
 ]

Seems to work totally fine.

How can I import the string the same way Import does?