1

I'm hitting an API with C# and getting response data like:

�y%9z�Pu�i�99��uB�2KRS�KKR����L,I��Z�����]&7��(&��� �O�/�+)��   �+��

The API specification says it works with Gzip content-encoding. Well if I make a request from Postman or Powershell(Invoke-RestMethod) with the exact same headers I can see the information clearly in JSON format, but not in C#

public static void Test()
{
    using (var client = new System.Net.Http.HttpClient() { BaseAddress = new Uri("https://xxx/") }) {
        using (var request_ = new System.Net.Http.HttpRequestMessage())
        {
            var auth = GetAuthHeader();
            var agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36";

            request_.Headers.TryAddWithoutValidation("Accept", "application/json");
            request_.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip");
            request_.Headers.TryAddWithoutValidation("Authorization", auth);
            request_.Headers.TryAddWithoutValidation("Customer-Session-Id", Guid.NewGuid().ToString());
            request_.Headers.TryAddWithoutValidation("User-Agent", agent);
            request_.Method = new System.Net.Http.HttpMethod("GET");
            var url_ = "XXX";
            request_.RequestUri = new Uri(url_, UriKind.RelativeOrAbsolute);

            var response = client.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, System.Threading.CancellationToken.None).Result;
            var arr = response.Content.ReadAsStringAsync().Result;

            Console.WriteLine(response.ToString());
            Console.WriteLine(arr);
        }
  

Do I need to do an extra encoding step?

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92

0 Answers0