'Decoding API data with RestClient
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);
Solution 1:[1]
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();
Solution 2:[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);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Samuel Dague |
| Solution 2 | Sergey Nazarov |
