Search code examples
apiuripowershell-3.0invoke-webrequest

How do I insert a variable into a uri using powershell?


Using Powershell, I'm attempting to take a variable and insert it into a uri which will be used to make an api call to a third party.

In the example below, I've set the variable being used along with the uri I'm using to make the call:

#variable that needs to be inserted into the uri
$data = $data.radiosetup

#uri that will be used to make the API call
$uri = "https://mickeymouse.tv.com/api/table/radio?sysparm_query=short_description%3D**variable goes here**&sysparm_limit=1"

I've tried the following:

"https://mickeymouse.tv.com/api/table/radio?sysparm_query=short_description%3D$data&sysparm_limit=1"
"https://mickeymouse.tv.com/api/table/radio?sysparm_query=short_description%3D$($data)&sysparm_limit=1"
"https://mickeymouse.tv.com/api/table/radio?sysparm_query=short_description%3D'$data'&sysparm_limit=1"

Any help with this would be appreciated. Thanks.


Solution

  • After further research, I learned you have to list the variable as a string as such:

    [string]$data = $($radio.setup)
    $uri = "https://mickeymouse.tv.com/api/table/radio?sysparm_query=short_description%3D$data&sysparm_limit=1"