47

How can I call a rest based API from a PowerShell script and process the Json answer?

user285677
  • 1,098
  • 3
  • 14
  • 21

3 Answers3

50

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.

Michiel
  • 468
  • 3
  • 23
Dave
  • 1,212
  • 12
  • 8
  • You might also need to supply credentials. In this case you would make a command such as $stuff = Invoke-RestMethod -Uri $url -Method Get -Credential "domain\username". – Dewi Rees Oct 08 '15 at 15:11
  • 1
    Is there a way to pass in the credentials for Basic Auth? What Jubblerbug said works to present a popup asking for the password but I need to automate this – Patrick Schaefer Apr 13 '17 at 16:47
22

I created this Get-Http function to make HTTP requests

param([string]$url)

$req = [System.Net.WebRequest]::Create($url)
$req.Method ="GET"
$req.ContentLength = 0

$resp = $req.GetResponse()
$reader = new-object System.IO.StreamReader($resp.GetResponseStream())
$reader.ReadToEnd()

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.

Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
5

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:

# Authentication
$webclient = New-Object System.Net.WebClient
$creds = New-Object System.Net.NetworkCredential("MyUsername","MyPassword");
$webclient.Credentials = $creds

# Data prep
$data = @{Name='Test';} | ConvertTo-Json

# GET
$webClient.DownloadString($url) | ConvertFrom-Json

# POST
$webClient.UploadString($url,'POST',$data)

# PUT
$webClient.UploadString($url,'PUT',$data)
ShaneC
  • 2,237
  • 2
  • 32
  • 53
  • this is powershell you use in production to consume web services? – Thufir Feb 16 '18 at 19:04
  • I was answering a specific question related to interacting with a JSON API through PowerShell.I never said I'd use this code in production. – ShaneC Feb 22 '18 at 16:38
  • 1
    I'm confused, you're implying that this is somehow not proper code. IF you would not use this in production, then why not? and why post it? – Larry Smith Sep 18 '18 at 15:03
  • 5
    I would not use this code in production because the credentials are hard-coded into the script as plain text. They should be script parameters and should be encrypted. This is just a sample script, it doesn't do anything, so I wouldn't put it into production because it doesn't do anything, other than answer the question that was asked here.I posted this in plain text because it is a simple answer to a simple question. – ShaneC Sep 19 '18 at 11:09