Product: PowerShell Universal Version: 4.2.20
Is it possible to use multiple Query Parameters with an API endpoint? Documentation/examples only ever show a single parameter and when I try using & in the Uri PSU returns an error saying ampersand is not allowed and is reserved for future use. This seems like basic functionality so I’m hoping that PSU just uses another character instead of the industry-standard ampersand.
Can you share your code? It sounds like you don’t have quotes around the invoke-restmethod URL and PS is interpreting the & incorrectly. Multiple query parameters are supported by PSU.
PS C:\Users\adamr> Invoke-WebRequest http://localhost:5000/myapi?query1=1&query2=2 Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 1 Job1 BackgroundJob Running True localhost Invoke-WebRequest http:/… query2=2: The term 'query2=2' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Here’s an example with a string.
PS C:\Users\adamr> Invoke-WebRequest "http://localhost:5000/myapi?query1=1&query2=2"
StatusCode : 200
StatusDescription : OK
Content : [{"Query2":"2","Query1":"1"}]
RawContent : HTTP/1.1 200 OK
Date: Wed, 11 Dec 2024 15:36:32 GMT
Server: Kestrel
Cache-Control: no-cache
Transfer-Encoding: chunked
Vary: Accept-Encoding
Request-Context: appId=
Content-Type: application/js…
Headers : {[Date, System.String[]], [Server, System.String[]], [Cache-Control, System.String[]],
[Transfer-Encoding, System.String[]]…}
Images : {}
InputFields : {}
Links : {}
RawContentLength : 29
RelationLink : {}
My endpoint looks like this.
param($Query1, $Query2)
@{
Query1 = $Query1
Query2 = $Query2
}Adam Driscoll
PowerShell Expert and Developer at Devolutions
Thanks, @Adam Driscoll! The quotes around the URL parameter is actually the issue. Appreciate the quick response! It would probably help to place a note in documentation that states these are required. Looks like PS5.1 and PS7 give different errors when these are not included:
PS5.1
PS7
d16206dce5fe106cda608f20ac702dabd6572d07.png
f587f6b6a2741428a72ecbf88db449a190da4aee.png