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;
}
The data is coming back compressed with gzip. You can have your
HttpClientautomatically decompress this data by enabling this property when instantiating yourHttpClient: