Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit e388077

Browse files
authored
Merge pull request #471 from om2804/fixAddQueryParam
fix handling urls endwith "&" or "?"
2 parents 73ae47a + c26d1d4 commit e388077

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/ServiceStack.Text/HttpUtils.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public static string AddQueryParam(this string url, object key, string val, bool
2929
public static string AddQueryParam(this string url, string key, string val, bool encode = true)
3030
{
3131
if (string.IsNullOrEmpty(url)) return null;
32-
var prefix = url.IndexOf('?') == -1 ? "?" : "&";
32+
var prefix = string.Empty;
33+
if (!url.EndsWith("?") && !url.EndsWith("&"))
34+
{
35+
prefix = url.IndexOf('?') == -1 ? "?" : "&";
36+
}
3337
return url + prefix + key + "=" + (encode ? val.UrlEncode() : val);
3438
}
3539

tests/ServiceStack.Text.Tests/HttpUtilsTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public void Can_AddQueryParam()
1313
Assert.That("http://example.com?f=1".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?f=1&f=2"));
1414
Assert.That("http://example.com?s=0&f=1&s=1".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?s=0&f=1&s=1&f=2"));
1515
Assert.That("http://example.com?s=rf&f=1".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?s=rf&f=1&f=2"));
16+
Assert.That("http://example.com?".AddQueryParam("f", "1"), Is.EqualTo("http://example.com?f=1"));
17+
Assert.That("http://example.com?f=1&".AddQueryParam("f", "2"), Is.EqualTo("http://example.com?f=1&f=2"));
1618
}
1719

1820
[Test]

0 commit comments

Comments
 (0)