I have been tasked with writing a certain Powershell script into Go. I do not believe I am at liberty to share many of the details of what this script needs to do, except this: the script makes a call to an API using Invoke-WebRequest
. The request is a GET
request with a JWT for authorization.
This request succeeds in Powershell but fails in Go due to some firewall issue. For some reason, the request is blocked when sent from Go but has no issues when sent from Powershell. There is a proxy, and that proxy is built into the Go http
client. I have been able to build and successfully use clients with this proxy in Go in this environment before. The Powershell request does not involve passing credentials to the proxy.
My hope is that by reviewing the request constructed by Go and comparing it against the request generated by Invoke-WebRequest
, I can identify what is missing from the Go request and make corrections accordingly. Currently, I can review the raw request in Go with httputil.DumpRequestOut()
, but I cannot find how to capture the raw request created and sent by Invoke-WebRequest
in Powershell. At least, I cannot find a way that satisfies the constraints of my situation.
I have seen solutions that involve the use of Fiddler, netsh
, and wireshark, but unfortunately I do not have any access to these tools in my environment. These all require elevated rights that I do not possess, and that I will not be granted. I have attempted to review the source code of Invoke-WebRequest
itself but have not found any help there either. I encountered a link to the Fiddler repo, which was encouraging, but the post with the link was seven years old and the repo no longer exists there.
Unless I am severely mistaken, the request must be constructed locally before it is sent, so there must be some way for one to access the request locally. How might I access the raw request generated by Invoke-WebRequest
?
nimrod's helpful answer shows a way to inspect a web request at its destination, via an online test service.
As for inspecting a request created by Invoke-WebRequest
at the source:
While I'm not aware of a way to do that directly, you can gain insights into the properties that go into formation of the request as follows, using the example of a simple GET
request to https://example.org
:
Add -Debug
to your Invoke-WebRequest
call:
Invoke-WebRequest https://example.org -Debug
You'll get a confirmation prompt:
[S] Suspend
You'll enter a nested debugging prompt, as evidenced by two >
chars. showing at the end of the prompt string (e.g., PS C:\temp>>
. Execute the following there and inspect the output:
$CurrentlyExecutingCommand
When you're done debugging, submit exit
and submit either A to continue and submit the request, or H to abort execution.
$CurrentlyExecutingCommand
is a (currently undocumented) automatic variable - available in a debugging session invoked for binary cmdlets only - that represents the instance of the .NET class that implements the cmdlet being invoked, which in the case of Invoke-WebRequest
is Microsoft.PowerShell.Commands.InvokeWebRequestCommand
.
While in the debugging prompt, you're then free to inspect this instance's properties, such as $CurrentlyExecutingCommand.WebSession
Note: