HttpClient Response returns scrambled data

599 views Asked by At

I'm trying to get a proper JSON response from this URL using HttpClient. When I view the URL in Chrome the data is properly formatted JSON. When I use the HttpClient, I get a bunch of junk data that looks like bytes or something like that. I can't figure out how to decode it into a string. Please advise.

string url = "https://api.nasdaq.com/api/calendar/earnings?date=2010-07-30";

string calendar = await DownloadFile(new string[] { url });

private static readonly HttpClient httpClient = new HttpClient();

        public static async Task<string> DownloadFile(string[] args)
        {
            string url = args[0];

            httpClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, br");
            httpClient.DefaultRequestHeaders.Connection.ParseAdd("keep-alive");

            string text = await httpClient.GetStringAsync(url);
            
            return text;

        }
3

There are 3 answers

1
Andy On

The data is coming back compressed with gzip. You can have your HttpClient automatically decompress this data by enabling this property when instantiating your HttpClient:

    private static readonly HttpClient httpClient = new HttpClient(new HttpClientHandler
    {
        AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
    });
0
Quelo On

I think you have to remove Brotli compression from your request header. Brotli (br) is not decoded out of the box by .Net.

That is, change:

httpClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate, br");

to

httpClient.DefaultRequestHeaders.AcceptEncoding.ParseAdd("gzip, deflate");
0
Reza Taba On

I've provided a solution with full code in this post for decompressing Gzip, Deflate, Brotli or All using AutomaticDecompression.

NOTE: You don't even have to send a header necessarily.

https://stackoverflow.com/a/77551050/3944285