Click here to Skip to main content
15,885,768 members
Articles / Programming Languages / PowerShell
Tip/Trick

Sending POST/GET Requests with PowerShell

Rate me:
Please Sign up or sign in to vote.
4.80/5 (3 votes)
6 Sep 2015CPOL 33K   8   1
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)


Written By
Software Developer
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionGreat! Pin
fatman457-Sep-15 12:51
professionalfatman457-Sep-15 12:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.