'.NET 6 - Pass JSON in query string

I use .NET 6, and I want to use HttpClient.GetAsync to send a query. One of the arguments is JSON, and I do not know how to encode it correctly.

Here is the documentation I use: https://docs.datadoghq.com/api/latest/metrics/#query-timeseries-points

I have tried with this method, but I got a BAD REQUEST from the server

    public async Task<string> MetricAggregateAsync(string from, string to, string query)
    {
        AddDDApiKey();
        AddDDApplicationKey();

        //var content = new StringContent(json, Encoding.UTF8, "application/json");
        //System.Web.Helpers.Json.Encore(query);

        query = System.Web.HttpUtility.UrlEncode(query);

        string content = "?from=" + from +"&to=" + to + "&query=" + query;

        HttpResponseMessage response = await _httpClient.GetAsync("v1/query" + content);

        response.EnsureSuccessStatusCode();

        return await response.Content.ReadAsStringAsync();
    }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source