Search code examples
c#httpclient

How to decompress a string coming from an API using HttpClient in C#?


I'm trying to consume a API with HttpClient.
But in the Body response I get a binary combination of characters which is not any compression algorithm:

Headers:

content-disposition: attachment; filename=1-45720792-4b25-31a5-b79a-45871e7a14ef
content-encoding: deflate
Content-Type: application/octet-stream
Expires: 0

Body:

x^uVMo?F►??W¶\<?♦)?I?\[??@♂$???☻?p%oE?K;B??????"!SÇ?y???3???←???????&?↨?????????????\*?(M??\\↑???▲J▼????U◄Ms??????,????!??:?Y?7{???o⌂????\>???6?\>▼?▼??o??eh‼?U?Z\]W???W☼????????u??&+?????♠!↕m♫/►'!.???Q↕?m?+???Y????z?J??#h?lB?4????@w5%?z4?y???↑'?`1E\?!???Y?|`?c4♀?\]E'?♦?x?`9xf8?☺?‼(??↔B☻O??     ]???N1n??Oq??0??S)?↓}???;KG?f???~☺(????►↑?1?    TE☼???O♫?%?%H?,?r^?         `????&K♦???Gh?(7?%??\`?L=♦♥♦????+♦→?←\<?▼? ?y???"?=2?☺(??N|???????uU??????????? ??^|??@?cs?#es☺(?x7?♫@?\]B??♦
3?f?→N ?3?%'Q??♫??%P??►2O??!?☻?7?¯♦P??)?←?¶?\]?#P??h??DB
?B?p^?6??♦?K?X??$9?
?       ???♦
?")?^??'↑?↕x?6??@??¶    ?       ?)???n?8n?J♥(????gj'A1jnj?E5?e?▼↔\<O?R▲?\\⌂?☺??r?s?ib? E?u?a???S.??P??/¶1'?&???E$?Xw??i??Tq?E)?:M←-???▬♫?S?F?jb??"fe§17?f?▲????Z?u2xo???f??↑??%?m3?Klobjx?????¶??h?↑7?\[??????w?☺?5?

It is for this reason that no algorithms such as deflate, gzip, etc. have been used for decompression.

This is the code:

HttpClient client = new HttpClient();

client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "milink_does_not_important");

request.Headers.Remove("Accept-Encoding");

HttpResponseMessage response = client.SendAsync(request).Result;

if (response.IsSuccessStatusCode)
{
   // Print the response
    Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}

I would like to decompress what comes in the response Body.


Solution

  • I solved it by just adding this:

    client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");