0

I am not entirely sure how to do this or really where to begin, I can't seem to find any information pointing me in the correct direction. I am calling an API and returning some data that is compressed?/Encoded. If I call the API and try to look at the data I see this.

My code to call the API

public static async Task<string> regGet(String RequestURL)
{
    var client = new RestClient(RequestURL);
    client.Timeout = -1;
    var request = new RestRequest(Method.GET);
    request.AddHeader("Content-Type", "multipart/mixed; boundary=Boundary_105363_1671506527_1650566832881");
    request.AddHeader("accept", "*/*");
    request.AddHeader("Authorization", "Bearer " + TokenManager.GetAccessTokenString("TRN"));
    IRestResponse response = client.Execute(request);
    Console.WriteLine(response.Content.ToString());
    return response.Content;
}

and what I see

> --Boundary_106430_1882288751_1650571711623
    Content-Encoding: deflate
    Content-ID: 1-00023d41-0618-3602-8e77-b89b271c7144
    dl_id: 1-00023d41-0618-3602-8e77-b89b271c7144
    dl_compression_type: deflated
    dl_document_name: MITBAL
    dl_document_date: 2021-07-30T03:15:12.628Z
    dl_document_indexed_date: 2021-07-30T03:15:14.647Z
    dl_message_id: 9d3db66d-696d-4e3f-bea8-ae4c6ae620d4
    dl_corrupt: false
    dl_size: 942
    dl_encoding: UTF-8
   
> x?]UM??6►??g?kP▼?-?hJ???"-R??K?&n?@vS$N???⌂?♀????`<♫??U7?⌂3az?m?=e?☻????=e?=`-?_i?☺??G?m??bY?O?q?§[????♦???????=@t?↑►#'`'??♥??♀?=↔◄???a?hY6??♫?6?<úXT?
    ^?????E♥?~Z?>?Y?☻s§???EK????it?nz?K?Z???\VE???§rp?!???y?l ?va?Z?↑??a??§?P♠?►???↔??1?~H?N◄r??????7z?????v?t]D*?&??V ??↓?►`?∟??^?M?s♫??`???♣?L{
    @?q????♦>3??e?-@
    ?/???)▲@d♀B?↑♣?_?i↨ ↔U? =C;J?%V?|@?∟▲?Z??g??♦?♣??N??0)??<?h?xN7v?H?L??>?S???A?;??▲@?]↔^ f?♦Fz??+?☺↕
    ??~►?,?▼??↕???♦????@>A[/??0D\9?5&J?y?pV ?↨t☼?Y?cO~????D?l#p?SK?↕4☺?;F?▲|?S$-?^i2X?????A?O???Y?k¶J^/W♀k??N~?i??n▼↓u???O2=???z?_?♦?K6;?g???0A?Nt??Js?↓¶?▲l?9??4▲TRT???_?Tk,?|}?????[?????/??▼??⌂v?????#?[??o__o??/??????_?M?Q?n?#M
    z???=no⌂??(?g?z.?g%♀??Y?^^ ??????)??q?????/?♥???^
    --Boundary_106430_1882288751_1650571711623--

Any ideas where to start?

I have tried to decode it, using the following code but i just get more encoded characters

Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
var result = encoding.GetString(response.RawBytes);
Progman
  • 16,827
  • 6
  • 33
  • 48
  • How to encode in your api? – Onurkan Bakırcı Apr 21 '22 at 20:17
  • 2
    Does this answer your question? [Handling HTTP ContentEncoding "deflate"](https://stackoverflow.com/questions/3932117/handling-http-contentencoding-deflate). For C#, this example should help: [DeflateStream Class](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.deflatestream?view=net-6.0) – paulsm4 Apr 21 '22 at 20:18

2 Answers2

2

It happens because your response.Content is a byte array. When you call ToString() it's just converted to string. To extract content we should decode it in right encoding.
As we can see it is UTF-8, because of dl_encoding: UTF-8.
Try next code:

var content = Encoding.UTF8.GetString(response.Content);
Sergey Nazarov
  • 671
  • 3
  • 15
  • Your suggestion will help prevent the OP from *CORRUPTING* the payload ... but he still probably wants to *READ* it. [DeflateStream](https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.deflatestream) is a good way to do this in C#., – paulsm4 Apr 22 '22 at 03:29
  • Yes, you are right. I missed something. – Sergey Nazarov Apr 22 '22 at 07:27
0

I finally got it, it was encoded with gzip, after i updated to the newest v107 restclient, i was able to decoded it in plain text so the following code return it in a readable format, thanks for all the help gentlemen!

  var options = new RestClientOptions(RequestUrl)
            {
                ThrowOnAnyError = true,
                Timeout = -1,
                AutomaticDecompression = DecompressionMethods.GZip

            };
            var client = new RestClient(options);
            client.Authenticator = auth.OAuth;
            var request = new RestRequest(RequestObject, Method.Get);

            foreach (var parameter in parameters)
            {
                request.AddParameter(parameter.Key, parameter.Value);
            }
            request.AddHeader("accept", "multipart/mixed");
            request.AddHeader("Accept-Encoding", "gzip");
            var response = await client.ExecuteGetAsync(request);
            return response.Content.ToString();