diff --git a/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs b/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs index ee53441..dcbf45b 100644 --- a/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs +++ b/DigitalData.Core.Abstractions/Client/IBaseHttpClientService.cs @@ -12,7 +12,7 @@ namespace DigitalData.Core.Abstractions.Client string? scheme = null, int? port = null, string path = "", - Dictionary? queryParams = null, + Dictionary? queryParams = null, HttpMethod? method = null, HttpContent? body = null, Dictionary? form = null, diff --git a/DigitalData.Core.Client/BaseHttpClientService.cs b/DigitalData.Core.Client/BaseHttpClientService.cs index 5d379f2..9b6b2ac 100644 --- a/DigitalData.Core.Client/BaseHttpClientService.cs +++ b/DigitalData.Core.Client/BaseHttpClientService.cs @@ -30,7 +30,7 @@ namespace DigitalData.Core.Client string? scheme = null, int? port = null, string path = "", - Dictionary? queryParams = null, + Dictionary? queryParams = null, HttpMethod? method = null, HttpContent? body = null, Dictionary? form = null, @@ -54,14 +54,25 @@ namespace DigitalData.Core.Client if (queryParams?.Any() ?? false) { var query = HttpUtility.ParseQueryString(uriBuilder.Query); - foreach (var param in queryParams) - query[param.Key] = param.Value; - uriBuilder.Query = query.ToString(); + var flagParams = queryParams.Where(param => param.Value is null).Select(param => HttpUtility.UrlEncode(param.Key)); + + var valueParams = queryParams.Where(param => param.Value is not null); + + foreach (var param in valueParams) + query[param.Key] = param.Value switch + { + bool b => b.ToString().ToLower(), + _ => HttpUtility.UrlEncode(param.Value.ToString()) + }; + + var flagQuery = string.Join(QUERY_SEPARATOR, flagParams); + + uriBuilder.Query = string.Join(QUERY_SEPARATOR, query.ToString(), flagQuery); } var requestUri = uriBuilder.Uri; - + Console.WriteLine(requestUri); var requestMessage = new HttpRequestMessage(method, requestUri); // Add headers if provided @@ -99,5 +110,7 @@ namespace DigitalData.Core.Client internal static readonly char[] URI_TRIM_CHARS = { '\\', '/', ' ' }; internal static string UriCombine(params string[] paths) => System.IO.Path.Combine(paths).Replace("\\", "/"); + + internal static readonly char QUERY_SEPARATOR = '&'; } } \ No newline at end of file diff --git a/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs b/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs index 8429419..d423f7f 100644 --- a/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs +++ b/DigitalData.Core.Tests/Client/BaseHttpClientServiceTest.cs @@ -35,7 +35,7 @@ namespace DigitalData.Core.Tests.Client [Test] public async Task FetchJsonAsync_ShouldReturnJsonResponse_WithQueryParams() { - var queryParams = new Dictionary + var queryParams = new Dictionary { { "id", "1" } };