-
Notifications
You must be signed in to change notification settings - Fork 1
/
Invoke-Api.ps1
45 lines (34 loc) · 1.35 KB
/
Invoke-Api.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Import-Module "$PSScriptRoot\AwsSigV4.psm1" -DisableNameChecking
Import-Module AWSPowerShell -DisableNameChecking
$ErrorActionPreference = "Stop"
#API Gateway endpoint
$BaseUri = 'https://azazazazaz.execute-api.us-east-1.amazonaws.com/petstore/pets'
#Get AWS Temporary credentials
#Use a profile name as needed
$Credentials = (Get-AWSCredential -ErrorAction Stop).GetCredentials()
#GET Request
#QueryString must be alphabetically ordered
$QueryString = "bar=foo&foo=bar"
$Headers = Get-AWS4SignatureHeaders `
-HTTPEndpoint $BaseUri `
-RequestParameters $QueryString `
-Method 'GET' `
-AwsAccessKey $Credentials.AccessKey `
-AWSSecretKey $Credentials.SecretKey `
-AwsToken $Credentials.Token
#The api includes the ? query string paramaters
$ApiEndpoint = "$($BaseUri)?$($QueryString)"
Invoke-RestMethod -Method 'GET' -Uri $ApiEndpoint -Headers $Headers
#POST Request
$Body = @{
"foo" = "bar"
}
$Payload = $Body | ConvertTo-Json
$Headers = Get-AWS4SignatureHeaders `
-HTTPEndpoint $BaseUri `
-RequestParameters $Payload `
-Method 'POST' `
-AwsAccessKey $Credentials.AccessKey `
-AWSSecretKey $Credentials.SecretKey `
-AwsToken $Credentials.Token
Invoke-RestMethod -Method 'POST' -Uri $BaseUri -Headers $Headers -Body $Payload