I am using powershell to get data using INCOKE-RestMethod and using GET method
So far i did this on PowerShell
$response = Invoke-RestMethod 'www.sample.net/data?key=1234&Accept=application/JSON&startdate=2023-08-11&endadte=2023-09-10' -Method 'GET' -Headers $Headers
$response | ConvertTo-Json -Depth 100 | Out-File "C:\Users\Admin\Desktop\sample.json" -Encoding UTF8
Works flawless - No complaints , However i tried to automate it so that i don't have to run every month.
So i tried this to test first -
$lastmonth11th = (Get-Date -Day 11).AddMonths(-1).ToString("yyyy-MM-dd")
$thismonth10th = (Get-Date -Day 10).ToString("yyyy-MM-dd")
$response_text = "'www.sample.net/data?key=1234&Accept=application/JSON&startdate="+$lastmonth11th+"&endadte="+$thismonth10th+"'"
Write-Output $lastmonth11th
Write-Output $thismonth10th
Write-Output $response_text
When i executed it - it gives me the result as i needed
2023-08-11
2023-09-10
'www.sample.net/data?key=1234&Accept=application/JSON&startdate=2023-08-11&endadte=2023-09-10'
But if i tried $response_text in Invoke-RestMethod it fails
$response = Invoke-RestMethod $response_text -Method 'GET' -Headers $Headers
$response | ConvertTo-Json -Depth 100 | Out-File "C:\Users\Admin\Desktop\sample.json" -Encoding UTF8
Invoke-RestMethod " Invalid URI : The host name could not be parsed ,
So i tried adding -Uri infront of $response_text - still no luck ,
WHat wrong i am doing? I have slim to none knowledge on PowerShell , Any help would be appraciated.
The problem is that you effectively add superfluous single quotes around your URL when you build it. I will explain. In the first (working) code you execute the
Invoke-RestMethodcmdlet, and your first parameter is the URL it is executing against in string format. You put single quotes around it, and that is a valid way to define a string.If you had assigned that string to a variable beforehand you would have done it the same way:
The value of
$URLdoes not include those single quotes, just like how you wouldn't put those quotes in a browser if you were trying to visit the web page. The value is simply:Now when you build your second URL you used double quotes to define the string, which is also a valid method to declare the value of a string. Within those double quotes you also included the a single quote at the beginning and end of the string. So, if you assign that URL to a variable like my example above it would look like this:
The value of
$URLexpands the variables, and joins the string parts together, so now the value of$URLlooks like this:Notice the change where previously the value did not have any quotes, and now it has single quotes. The solution for you is to remove the single quotation mark from the beginning and end. Since you are using double quotes it will expand the simple string variables so you don't have to do the whole
"abc"+$VarA+"def"thing, and just have it all within the double quotes. With that in mind defining$URLwould look like this:And then the value of
$URLlooks similar to my first example: