Invoke-RestMethod converts backslash to forward slash

I would like to call a REST API by passing in a parameter containing a backslash character.

The API expects a route type api/test/<app_name>/<user_name>

, and for example I would like to pass domain\login

as a value for user_name

.

I tried this using PowerShell:

$uri = [Uri]::EscapeUriString("http://localhost:5555/api/test/App Name/Domain\login")
# now $uri = 'http://localhost:5555/api/test/App%20Name/Domain%5Clogin'
Invoke-RestMethod -Method Get -Uri $uri -Verbose

      

The output Invoke-RestMethod

with the flag -Verbose

shows that PowerShell is converting the backslash to a forward slash , after which I get a 404 from the REST API (which is expected since the route doesn't match):

VERBOSE: GET http://localhost:5555/api/test/App Name/Domain/login with 0-byte payload
Invoke-RestMethod :
404 Not Found

      

How can I avoid this behavior and use a backslash encoded path?

Here is the result of my variable $PSVersionTable

:

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14393.953
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.953
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

      

+3


source to share


1 answer


What to say: replace /

with //

Maybe you should define it as a regular String and then convert it:

$RawUri = 'http://...'

      



Here you should use //

instead /

.

Then use your code and replace the text with the new variable.

0


source







All Articles