What is the best way to send HTTP requests from Windows Powershell?
The Invoke-WebRequest cmdlet sends HTTP and HTTPS requests to a web page or web service. It parses the response and returns collections of links, images, and other significant HTML elements. This cmdlet was introduced in PowerShell 3.0.
To call a REST API from the Windows PowerShell, you should use the Invoke-RestMethod cmdlet. A call to an API is simply a request through HTTP or HTTPS. So, you will need a URL to which the API will be sent. You can find detailed information about the URL to call to get data from API documentation.
When you call a REST API from PowerShell, you will use the cmdlet Invoke-RestMethod. To get a complete overview of this cmdlet, click here. If you want to practice calling specific APIs, you can use tools like Postman or the REST client VSCode extention.
iex is an alias for Invoke-Expression . Here the two backticks don't make any difference, but just obfuscates the command a little. iex executes a string as an expression, even from pipe. Here Start-Process is a cmdlet that starts processes.
Found one way:
$page = (New-Object System.Net.WebClient).DownloadString("http://localhost/")
Thanks to Steven Murawski for his comment:
The best way really depends on what task you are trying to accomplish as the two answers below have noted. WebClient is the simplest, but HttpWebRequest is the most flexible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With