Decompressing Web Api response

I am compressing web Api responses with following config

<system.webServer>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
  <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
  <dynamicTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="application/*" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </dynamicTypes>
  <staticTypes>
    <add mimeType="text/*" enabled="true" />
    <add mimeType="application/*" enabled="true" />
    <add mimeType="*/*" enabled="false" />
  </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />

Now when i consume this in a Win Form applications and try to do the follwoing

var rawData = await response.Content.ReadAsStringAsync();
                   var deserializedData = JsonConvert.DeserializeObject<Employees[]>(rawData).ToList();

it fails on

var deserializedData = JsonConvert.DeserializeObject(rawData).ToList();

the Error message is

{"Unexpected character encountered while parsing value: . Path '', line 0, position 0."}

I guess this is due to the fact that content is gziped and not being deserialized. Can anyone suggest a solution ? This works locally fine as local IIS is not gzip enabled

Upvotes: 0

Views: 334

Answers (1)

Alexander Polyankin
Alexander Polyankin

Reputation: 1887

You need to enable automatic GZip decompression:

var handler = new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip };
var client = new HttpClient(handler);

Upvotes: 1

Related Questions