Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
54 lines (41 sloc) 1.58 KB
author comments layout slug title desc tags
Joe Kampschmidt
true
post
invoke-restmethod-powershell-examples
Simple Examples of PowerShell's Invoke-RestMethod
Consume a REST API with Windows PowerShell using the Invoke-RestMethod function with these practical GET, PUT, POST and DELETE examples.
rest
powershell
api

The documentation for Invoke-RestMethod is a long sea of text. Skip it. These simple examples should get your started with consuming a REST API with PowerShell. Just a quick note that Invoke-RestMethod will parse the HTTP response for you and return a PowerShell object.

Simple GET example

$response = Invoke-RestMethod 'http://example.com/api/people'
# assuming the response was in this format { "items": [] }
# we can now extract the child people like this
$people = $response.items

GET with custom headers example

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-DATE", '9/29/2014')
$headers.Add("X-SIGNATURE", '234j123l4kl23j41l23k4j')
$headers.Add("X-API-KEY", 'testuser')

$response = Invoke-RestMethod 'http://example.com/api/people/1' -Headers $headers

PUT/POST example

$person = @{
    first='joe'
    lastname='doe'
}
$json = $person | ConvertTo-Json
$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Put -Body $json -ContentType 'application/json'

DELETE example

$response = Invoke-RestMethod 'http://example.com/api/people/1' -Method Delete