How to Post Request in PowerShell
There are different unique commands that can be used with PowerShell different scripts and functions in automation processes.
However, users often require support in identifying how to use PowerShell commands as its commands are unique in some cases when compared to other scripting and automation languages.
This article will briefly discuss how to POST
requests in PowerShell.
Use the POST
Method and Pass the Parameters in PowerShell
There are different methods to use POST
with the PowerShell to identify and post parametrized data to a URI
.
Use a Hash Table to Pass the Data in PowerShell
You can use a hash table to include all the parameters you need to pass. This can be done as a separate line of a hash table or within the Invoke-WebRequest
line.
Then you have to pass the values along with the URI
to invoke the POST
method and pass the parameters.
Command:
Invoke-WebRequest -Uri http://wwww.addyourwebsite.com -Method POST -Body @{username = 'xyz'; moredata = 'abc' }
The hash table with parameters you want to pass is within the curly brackets.
Output:
Use Invoke-WebRequest
With JSON
There are methods with additional requirements other than using the hash table to send parameters to a URI
with PowerShell. This next method would be to use Invoke-WebRequest
with the JSON type as some websites would require the data to be passed as JSON.
Command:
Invoke-WebRequest -UseBasicParsing http://wwww.addyourwebsite.com -ContentType "application/json" -Method POST -Body "{ 'Code':8789798, 'ID':'rter'}"
This would work with URI
, which also requires JSON parse.
Output:
Therefore, in these simple methods, it is possible to achieve the required outputs by passing parameters to URI
as needed by users.
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.