Skip to content

Commit f6d41a7

Browse files
committed
(maint) Add new query string builder function
1 parent 215193d commit f6d41a7

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function New-HttpQueryString
2+
{
3+
#Shamelessly taken from https://powershellmagazine.com/2019/06/14/pstip-a-better-way-to-generate-http-query-strings-in-powershell/
4+
[CmdletBinding()]
5+
param
6+
(
7+
[Parameter(Mandatory = $true)]
8+
[String]
9+
$Uri,
10+
11+
[Parameter(Mandatory = $true)]
12+
[Hashtable]
13+
$QueryParameter
14+
)
15+
# Add System.Web
16+
Add-Type -AssemblyName System.Web
17+
18+
# Create a http name value collection from an empty string
19+
$nvCollection = [System.Web.HttpUtility]::ParseQueryString([String]::Empty)
20+
21+
foreach ($key in $QueryParameter.Keys)
22+
{
23+
$nvCollection.Add($key, $QueryParameter.$key)
24+
}
25+
26+
# Build the uri
27+
$uriRequest = [System.UriBuilder]$uri
28+
$uriRequest.Query = $nvCollection.ToString()
29+
30+
return $uriRequest.Uri.OriginalString
31+
}

0 commit comments

Comments
 (0)