Powershell script to send POST Requests to API


The sample powershell script will help us to send a POST request to the API and get the response back from the application. This would also catch the exception in case of failure and log it accordingly

Running the Sample

Step 1: Open the PSASPNETPOSTRequestToAPI.ps1 in Administrator mode from the Powershell ISE.
Step 2: Then modify the request body in the script accordingly
Step 3: Update the URL with appropriate hostname and URI
Step 4: Execute the powershell script from Powershell ISE

Using the Code

The following code snippet is the sample of Script that serves the above requirement

Sending my RequestBody with Request Headers over POST request to the API
$endPointURL = "http://hostname:80/myUri" 
$webRequest = [System.Net.HttpWebRequest]::Create($endPointURL) 
Write-Host "Sending POST Request"+$endPointURL 
$webRequest.Timeout = 60000; 
$webRequest.ContentType="application/xml"$webRequest.Method= 'POST'$Body = [byte[]][char[]]$requestBody$Stream = $webRequest.GetRequestStream(); 

More Information

The following article will give information about powershell scripting:



Microsoft All-In-One Script Framework is an automation script sample library for IT Professionals. The key value that All-In-One Script Framework is trying to deliver is Scenario-Focused Script Samples driven by IT Pros' real-world pains and needs. The team is monitoring all TechNet forums, IT Pros' support calls to Microsoft, and script requests submitted to TechNet Script Repository. We collect frequently asked IT scenarios, and create script samples to automate the tasks and save some time for IT Pros. The team of All-In-One Script Framework sincerely hope that these customer-driven automation script samples can help our IT community in this script-centric move.