0

I've seen some people using HttpUtility.UrlEncode on each one of the parameters. Do I need to do the same in the GetCashBalanceAsync method below which uses RestSharp?

private static string BuildQueryString(Dictionary<string, object> queryParameters)
{
    return string.Join("&", queryParameters.Select(kvp =>
        string.Format("{0}={1}", kvp.Key, HttpUtility.UrlEncode(kvp.Value.ToString()))));
}

Code

public async Task<AccountInformation?> GetCashBalanceAsync()
{
    var queryString = $"timestamp={GetNonce()}";
    var endpoint = $"/api/v3/account?{queryString}&signature={AuthenticationToken(queryString)}";
    RestRequest request = new(endpoint);
    request.AddHeader("X-MBX-APIKEY", _apiKey);

    var response = await ExecuteRestRequestAsync(request).ConfigureAwait(false);
    if (response.StatusCode != HttpStatusCode.OK)
    {
        throw new Exception(
            $"GetCashBalance: request failed: [{(int)response.StatusCode}] {response.StatusDescription}, Content: {response.Content}, ErrorMessage: {response.ErrorMessage}");
    }

    var deserialized = JsonSerializer.Deserialize<AccountInformation>(response.Content!);
    return deserialized;
}
nop
  • 4,711
  • 6
  • 32
  • 93
  • 2
    Why don't you use `request.AddQueryParameter("foo", "bar")` for each of your params so you don't have to merge and manually encode them? – Marco Jun 07 '22 at 14:00
  • 1
    @Marco, that's a solution too – nop Jun 07 '22 at 14:19

0 Answers0