Click here to Skip to main content
13,597,485 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

23.9K views
13 bookmarked
Posted 24 Nov 2014
Licenced CPOL

PowerShell: Http Get/Post

, 24 Nov 2014
Rate this:
Please Sign up or sign in to vote.
How to use PowerShell to carry out REST operations such as GET/POST

This is another article in my on going learning/experimenting with PowerShell. This time, I will show how you can use PowerShell to carry out REST operations such as GET/POST.

Now there may be some amongst you, who go why didn’t you just use WGet, which is a downloadable thing, and I could have indeed used that, except for the part that the machine I need to run this on, is so locked down that I can only use things that are already installed. So raw PowerShell it is.

Here is the relevant PowerShell code, which allows 3 parameters to control the script:

  • Target: The url
  • Verb: This is the http verb, GET, PUT, etc.
  • Content: This could be some content when doing a POST request for example

It just makes use of some very standard .NET classes namely WebRequest.

[CmdletBinding()]
Param(
  
  
    [Parameter(Mandatory=$True,Position=1)]
    [string] $target,
  
    [Parameter(Mandatory=$True,Position=2)]
    [string] $verb,      
  
    [Parameter(Mandatory=$False,Position=3)]
    [string] $content
  
)
  
  
write-host "Http Url: $target"
write-host "Http Verb: $verb"
write-host "Http Content: $content"
  
  
  
$webRequest = [System.Net.WebRequest]::Create($target)
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($content)
$webRequest.Method = $verb
  
write-host "UTF8 Encoded Http Content: $content"
if($encodedContent.length -gt 0) {
    $webRequest.ContentLength = $encodedContent.length
    $requestStream = $webRequest.GetRequestStream()
    $requestStream.Write($encodedContent, 0, $encodedContent.length)
    $requestStream.Close()
}
  
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
if($resp -ne $null) 
{
    $rs = $resp.GetResponseStream();
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
    [string] $results = $sr.ReadToEnd();
  
    return $results
}
else
{
    exit ''
}

Here is an example usage (assuming the above script is saved as http.ps1 somewhere):

Http.ps1 - target "http://www.google.com" -verb "GET"

which would give the following sort of output:

Anyway that’s it for now, hope this helps!

License

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

Share

About the Author

Sacha Barber
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

You may also be interested in...

Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180621.3 | Last Updated 25 Nov 2014
Article Copyright 2014 by Sacha Barber
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid