Created
May 27, 2022 13:36
-
-
Save rnemeth90/bbb452aaa193c7d7d55bf37159ec3f06 to your computer and use it in GitHub Desktop.
Simple Examples of Invoking Rest Endpoints with Powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment