How can I make Invoke-RestMethod use the default web proxy?

Viewed 24131

The following works fine on my machine which does not use a web proxy.

return Invoke-RestMethod 
   -Uri $server$url 
   -ContentType $contentType 
   -Headers $headers 
   -Method $method 
   -UseDefaultCredentials 

Note: the $server$url is an https address, something like https://somewhere.example.com/api/data

Now I'm trying to get it to work in a corporate environment but I am getting a 401 error.

I think this is because there is a corporate proxy which is defined with a proxy.pac file. I have confirmed I can get to the $server$url URI from Internet Explorer. What do I need to do to get the Invoke-RestMethod command to work using the same settings?

I have tried adding the -proxy parameter

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

return Invoke-RestMethod 
    -Uri $server$url 
    -ContentType $contentType 
    -Headers $headers 
    -Method $method 
    -UseDefaultCredentials 
    -Proxy $proxy 
    -ProxyUseDefaultCredentials

but the -Proxy parameter is expecting a URI not an IWebProxy object.

2 Answers
Related