1

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.

GSerg
  • 76,472
  • 17
  • 159
  • 346
  • Does this answer your question? [How Decompress Gzipped Http Get Response in c#](https://stackoverflow.com/questions/48579425/how-decompress-gzipped-http-get-response-in-c-sharp) – Hunter Tran Jun 13 '23 at 16:04
  • 1
    Does the API Specification what kind of content it is providing? The Body you provided could be anything from an encryptet text to a png image... – LokleLama Jun 13 '23 at 16:06
  • 1
    You would normally use the headers to figure out how to handle the content. Unfortunately the response is so generic it's next to useless. What type of content are you expecting. It looks like a file containing binary data of some sort but beyond that, we're not going to be able to help you. – phuzi Jun 13 '23 at 16:13
  • 2
    This is not the body, this is garbage that you get when you try to read binary content as string with `ReadAsStringAsync`. – GSerg Jun 13 '23 at 16:43
  • looks like you are receiving binary content, refer to this article https://learn.microsoft.com/en-us/answers/questions/525301/web-api-2-returning-binary-file-with-application-o – Nonik Jun 13 '23 at 16:46

1 Answers1

1

I solved it by just adding this:

client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip");
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77