REST API dans PowerShell
Quand on utilise PowerShell dans Windows par défaut on n’a pas de curl. Si on utilise REST plusieurs exemples sont fait pour curl.
Heureusement PowerShell a la commande Invoke-RestMethod.
Voici quelques exemples simples :
Simple GET exemple
$response = Invoke-RestMethod 'http://example.com/api/data'
GET avec headers customise
$message = 'Message' $secret = 'secret' $hmacsha = New-Object System.Security.Cryptography.HMACSHA256 $hmacsha.key = [Text.Encoding]::UTF8.GetBytes($secret) $signature = $hmacsha.ComputeHash([Text.Encoding]::UTF8.GetBytes($message)) $encb64=[System.Convert]::ToBase64String($signature) $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("X-DATE", '9/29/2019') $headers.Add("X-SIGNATURE", $encb64) $headers.Add("X-API-KEY", 'testuser') $response = Invoke-RestMethod 'http://example.com/api/data/1' -Headers $headers
PUT/POST exemple
$person = @{ first='joe' lastname='doe' } $json = $person | ConvertTo-Json $response = Invoke-RestMethod 'http://example.com/api/data/1' -Method Put -Body $json -ContentType 'application/json'
DELETE exemple
$response = Invoke-RestMethod 'http://example.com/api/data/1' -Method Delete
Plus d’information dans la documentation officielle: Invoke-RestMethod.
Laisser un commentaire
Vous devez vous connecter pour publier un commentaire.