Powershell: Invoke-WebRequest and Invoke-RestMethod should honor HTTP_Proxy and HTTPS_proxy env vars

Created on 8 Feb 2017  路  15Comments  路  Source: PowerShell/PowerShell

The Unix way to specify proxy seems to be via HTTP_Proxy and HTTPS_Proxy env vars

Area-Cmdlets-Utility Area-Maintainers-Documentation Resolution-Fixed

Most helpful comment

@jakara I will consider possibly adding support for this in iwr and irm for the 6.2.0 release, it's a bit too late in the cycle for 6.1.0.

In the mean time this is a kind of work around that works cross platform:

$PSDefaultParameterValues["invoke-webrequest:proxy"] = $env:http_proxy

Add that to your profile or in the initialization of your scripts.

All 15 comments

The Windows way is to honour the system proxy in control panel -> internet options -> connections -> LAN settings.

The Windows way should already been honored by .Net on Windows

@SteveL-MSFT Shouldn't the *nix way be honored by .NET as well?

@joeyaiello yes, looks like CoreFx supports whatever libcurl supports (https://github.com/dotnet/corefx/issues/11757) so we may just need to also document this

Invoke-RestMethod was not using my system proxy today (PS5, Win7) I had to use the -Proxy parameter.

As far as I can tell, the web cmdlets never used the system proxy on Windows so having them suddenly start using the system proxy would be a breaking change. Need someone to do a bit more research on this so we don't have a breaking change.

If you have a system proxy (or, more likely a .pac) then all your internet traffic is going through those rules anyway. That is, according to the .pac, it will use a proxy or not as appropriate. To be clear: when I say PowerShell should respect the proxy I don't mean it should use a fixed proxy host, it should respect how the system is configured to use, or not, a proxy. Used properly, I can't see how it would break things: anything you can't access by respecting the proxy, you can't access in Chrome, IE etc.

Ran into a similar issue and ended up defining $env:HTTP_PROXY and $env:HTTPS_PROXY with the URL of http://:@:. This seems to be a quick workaround, at least on my CentOS test box.

as native curl and wget is coming to windows, I dont see the behavior of iwr will be changed at all:
curl an wget will honor the unix way, am I right?

@jakara I will consider possibly adding support for this in iwr and irm for the 6.2.0 release, it's a bit too late in the cycle for 6.1.0.

In the mean time this is a kind of work around that works cross platform:

$PSDefaultParameterValues["invoke-webrequest:proxy"] = $env:http_proxy

Add that to your profile or in the initialization of your scripts.

@markekraus that's great!

Just to clarify, given that the variable names in this issue's title used mixed case: on Unix-like platforms, where case matters, these variables are commonly _lowercase_ (http_proxy, https_proxy).

This was implemented in .Net 3+ in new HttpClient.

@sdwheeler Was this already documented for the web cmdlets?

@iSazonov Yes, this is documented.

Was this page helpful?
0 / 5 - 0 ratings