Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ninmonkey/f45594d0a223c64696bc74ce6cc3a28e to your computer and use it in GitHub Desktop.

Select an option

Save ninmonkey/f45594d0a223c64696bc74ce6cc3a28e to your computer and use it in GitHub Desktop.
Stand-Alone test to reproduce similar errors with summary

More info / Docs :

OP was using the same Invoke-WebRequest|RestMethod code on Ps5 and Ps7 with different results.

  • One returned text
  • The other was still gzipped

Here's a standalone test for 5 and 7. By changing, or omitting AutomaticDecompression -- you can reproduce similar output. The first link compares additional headers:

  • Content-Type
  • Content-Encoding
  • Accept-Encoding

Trigger

Accept-Encoding: gzip, deflate, br

For 5

  • works when AutomaticDecompression is Gzip
  • breaks when AutomaticDecompression is Deflate or not set
$handler = [Net.Http.HttpClientHandler]::new()
# $handler.AutomaticDecompression = [Net.DecompressionMethods]::GZip
# $handler.AutomaticDecompression = [Net.DecompressionMethods]::Deflate
$client = [Net.Http.HttpClient]::new( $handler )
$response = $client.GetAsync('https://postman-echo.com/gzip').GetAwaiter().GetResult()
$response.Content.Headers
$response.Content.ReadAsStringAsync().GetAwaiter().GetResult()

For 7

  • breaks when AutomaticDecompression is not set
  • works when AutoMaticDecompression is All or Gzip
 $handler = [Net.Http.HttpClientHandler]::new()
 # $handler.AutomaticDecompression = [Net.DecompressionMethods]::GZip
 # $handler.AutomaticDecompression = [Net.DecompressionMethods]::All

 $client = [Net.Http.HttpClient]::new( $handler )
 $response = $client.GetAsync('https://postman-echo.com/gzip').GetAwaiter().GetResult()
 $response.Content.Headers
 $q = $response.Content.ReadAsStream()
 [Text.Encoding]::GetEncoding('utf-8').GetString( $q.ToArray() )

Assembly Versions?

WinPS5> [Net.Http.HttpClientHandler].Assembly

GAC    Version        Location
---    -------        --------
True   v4.0.30319     C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll

Pwsh7.4.5>  [Net.Http.HttpClientHandler].Assembly.FullName
System.Net.Http, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment