How can I call a rest based API from a PowerShell script and process the Json answer?
asked Aug 26 ’10 at 11:43
1,01022 gold badges1414 silver badges2020 bronze badges
What you want is PowerShell 3 and its Invoke-RestMethod, ConvertTo-Json, and ConvertFrom-Json cmdlets. Your code will end up looking like:
$stuff = Invoke-RestMethod -Uri $url -Method Get;
and there shouldn’t even be a need to invoke ConvertFrom-Json on the resulting $stuff => it’s already in a usable non-string format.
See http://technet.microsoft.com/en-us/Library/hh849971.aspx for details.
43333 silver badges1919 bronze badges
answered Nov 6 ’12 at 23:39
1,1641111 silver badges88 bronze badges
I created this Get-Http function to make HTTP requests
$req = [System.Net.WebRequest]::Create($url)
$req.ContentLength = 0
$resp = $req.GetResponse()
$reader = new-object System.IO.StreamReader($resp.GetResponseStream())
Dealing with the end result as xml is really easy, however, if you want to process JSON you probably will need some .Net library like JSON.Net.
answered Aug 26 ’10 at 13:34
Darrel MillerDarrel Miller
120k2828 gold badges175175 silver badges231231 bronze badges
We use Powershell to query a REST API that deals only with Json style data. It was awkward at first but the below code is all we need to perform most operations:
$webclient = New-Object System.Net.WebClient
$creds = New-Object System.Net.NetworkCredential(“MyUsername”,”MyPassword”);
$webclient.Credentials = $creds
# Data prep
$data = @ | ConvertTo-Json
$webClient.DownloadString($url) | ConvertFrom-Json
answered Jul 10 ’15 at 8:01
1,85422 gold badges2121 silver badges5151 bronze badges