Trying to get Invoke-Webrequest not to escape URI

I’m trying to access a URL of a network switch to get some serial number info and what not. The URL for the switch is this:
hxxp://192.168.0.1/wcd?{DeviceBasicInfo}

In the browser this returns XML. Alright sounds easy enough, I run through powershell command like so:
Invoke-Webrequest “hxxp://192.168.0.1/wcd?{DeviceBasicInfo}”

Did not get the expected result. I powered up Fiddler to check and it’s encoding the curly braces { }. So the resulting URL is:
hxxp://192.168.0.1/wcd?%7BDeviceBasicInfo%7D

Putting that encoded result in a browser I get the same generic error result as powershell. The switch needs to have the { } braces in the URL for it to work. I looked at the URI constructor in C# just to be curious and they do have one with a flag with dontEscape boolean but that’s been obsoleted (here).

Has anyone ever encountered this situation before?

Nelis249,
Welcome to the forum. :wave:t3:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

Have you tried to use single quotes?

Invoke-Webrequest 'hxxp://192.168.0.1/wcd?{DeviceBasicInfo}'

Here you can read more about that topic:

Please always read the help topics COMPLETELY including the examples!!! :point_up_2:t3:

Yes I tried single quotes. I’ve been able to isolate it a simple test using the URI object (as Invoke-Webrequest is just converting it to that object)

PS D:\> $uri = [URI]'http://192.168.0.1/wcd?{DeviceBasicInfo}'
PS D:\> $uri


AbsolutePath   : /wcd
AbsoluteUri    : http://192.168.0.1/wcd?%7BDeviceBasicInfo%7D
LocalPath      : /wcd
Authority      : 192.168.0.1
HostNameType   : IPv4
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /wcd?%7BDeviceBasicInfo%7D
Segments       : {/, wcd}
IsUnc          : False
Host           : 192.168.0.1
Port           : 80
Query          : ?%7BDeviceBasicInfo%7D
Fragment       :
Scheme         : http
OriginalString : http://192.168.0.1/wcd?{DeviceBasicInfo}
DnsSafeHost    : 192.168.0.1
IdnHost        : 192.168.0.1
IsAbsoluteUri  : True
UserEscaped    : False
UserInfo       :

The AbsoluteUri is being encoded (or escaped).

URL encoding of special characters is an important part of the whole system. The browsers deal with it fine just like everything else. Seems your device is the problem here, not accepting/knowing how to handle the encoded URL.

Just look at what google shows you searching with this URL

https://www.google.com/search?q=a+search+with+%7B+and+%7D

Even this forum converts this encoding to human readable characters for us, i had to post it as code

https://www.google.com/search?q=a+search+with+{+and+}

Can encode the spaces as well and get the same result in the browser

https://www.google.com/search?q=a%20search%20with%20%7B%20and%20%7D
1 Like

I’m not disagreeing with you @krzydoug, the problem is the switch.

http://192.168.0.1/wcd?{DeviceBasicInfo}
[returns XML I'm expecting]
http://192.168.0.1/wcd?%7BDeviceBasicInfo%7D
[returns error msg]

So I’m trying to find a way to do it. It’s odd they had the functionality with the dontEscape flag that was there and hence removed.

What do you mean it was removed?

[uri]::new('http://192.168.0.1/wcd?{DeviceBasicInfo}',$true)

AbsolutePath   : /wcd
AbsoluteUri    : http://192.168.0.1/wcd?{DeviceBasicInfo}
LocalPath      : /wcd
Authority      : 192.168.0.1
HostNameType   : IPv4
IsDefaultPort  : True
IsFile         : False
IsLoopback     : False
PathAndQuery   : /wcd?{DeviceBasicInfo}
Segments       : {/, wcd}
IsUnc          : False
Host           : 192.168.0.1
Port           : 80
Query          : ?{DeviceBasicInfo}
Fragment       :
Scheme         : http
OriginalString : http://192.168.0.1/wcd?{DeviceBasicInfo}
DnsSafeHost    : 192.168.0.1
IdnHost        : 192.168.0.1
IsAbsoluteUri  : True
UserEscaped    : True
UserInfo       :

Try

$uri = [uri]::new('http://192.168.0.1/wcd?{DeviceBasicInfo}',$true)
Invoke-WebRequest $uri
1 Like

Holy smokes, how did I miss that. I guess I was thinking if the dontEscape flag was obsoleted in .NET so powershell wouldn’t do it either.

Thank you for the assistance.

That work? Obsolete and deprecated don’t necessarily mean unavailable. If you ever want to know about what overloads are available, just provide the method without parenthesis. It will list all the Overloads for that method/constructor

[uri]::new

OverloadDefinitions
-------------------
uri new(string uriString)
uri new(string uriString, bool dontEscape)
uri new(uri baseUri, string relativeUri, bool dontEscape)
uri new(string uriString, System.UriKind uriKind)
uri new(uri baseUri, string relativeUri)
uri new(uri baseUri, uri relativeUri)
1 Like

Yep it worked. Thanks agian.

1 Like