Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / PowerShell

Sending POST/GET Requests with PowerShell

4.80/5 (3 votes)
6 Sep 2015CPOL 34K  
This tip shows one of the ways to send a request from your machine to server using Windows PowerShell.

Preface

I decided to share this tip, as I was developing some application and I needed to test the user blocking by server when someone else logged with the same account on another machine.

At first, I had two machines and I was logging in and out manually, but after a while I wondered if there is a better way for doing it – that's where I found this PowerShelll feature, which is really nice.

All that is shown here can be written in a notepad, simply save it with ps1 extension or run it straight from the PowerShell.

PowerShell Script with POST

PHP
#this is the url that you want will send thae request to
$url = "www.this-is-your-url"
#here you can set your POST params
$account = "pavelDurov"
$password = "123456"

$parameters = "accountName=" + $account + "&" + "password="+ $password
#creating the xmlHtpp system object              
$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('POST', $url, $false)
#Setting required header of the request
$http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
$http_request.setRequestHeader("Content-length", $parameters.length)
#Assigning the params to the request
$http_request.send($parameters)
#printing the request result
echo $http_request.statusText

Similarly You Can Send with GET Method

PHP
#this is the url that you want will send thae request to
$url = "https://this-is-your-url.com"

#creating the xmlHtpp system object              
$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('GET', $url, $true)
#Sending the request
$http_request.send()

#printing the request result
echo $http_request.statusText

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)