tmenier / Flurl

Fluent URL builder and testable HTTP client for .NET
https://flurl.dev
MIT License
4.23k stars 387 forks source link

Encode Issue #822

Closed mfarid2 closed 6 months ago

mfarid2 commented 7 months ago

Flurl.Http 4.0.2

var today = DateTime.Today.ToString("yyyy-MM-dd");

var url = "https://test.com"
    .AppendPathSegment("folder1/folder2")
    .SetQueryParams(new
    {
        date_start = WebUtility.UrlEncode(today + "@date"),
        date_end = WebUtility.UrlEncode(today + "@date")
    });

Expected result:

https://test.com/folder1/folder2?date_start=2024-04-25%40date&date_end=2024-04-25%40date where @ = %40

Result:

https://test.com/folder1/folder2?date_start=2024-04-25%2540date&date_end=2024-04-25%2540date where @ = %2540

SemaphoreSlim1 commented 6 months ago

I'm not sure this is a bug. I think your problem is you're double encoding; you're url encoding your values, and then Flurl is doing it for you again. Check out this documentation

Try this instead:

var url = "https://test.com"
    .AppendPathSegment("folder1/folder2")
    .SetQueryParams(new
    {
        date_start = $"{today}@date",
        date_end = $"{today}@date"
    });

OR, if you don't like how Flurl encodes it and you want to do it manually, try this:

var url = "https://test.com"
    .AppendPathSegment("folder1/folder2")
    .SetQueryParam("date_start", WebUtility.UrlEncode($"{today}@date"),true)
    .SetQueryParam("date_start", WebUtility.UrlEncode($"{today}@date"),true);
mfarid2 commented 6 months ago

@SemaphoreSlim1 Yes. You are right. Double encoding happened. But it happens without WebUtility.UrlEncode() too. I did took your approach with some change.

var url = "https://test.com"
    .AppendPathSegment("folder1/folder2")
    .SetQueryParam("date_start", today + "%40date", true)
    .SetQueryParam("date_end", today + "%40date", true);

Many thanks.