RestAPI Basics mit Powershell

Einige Scripte zur Nutzung von RestAPI mittels Powershell v3.

Einfacher Aufruf einer URL mit der Funktion „Invoke-WebRequest“ und Anzeige der Rückgabe:

1
2
3
4
#REQUIRES -Version 3
$url ="https://jsonplaceholder.typicode.com/todos/1"
$request = Invoke-WebRequest $url
$request

 

Einfacher Aufruf einer URL mit der Funktion „Invoke-WebRequest“ sowie etwas Plausibilitätskontrolle

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
#REQUIRES -Version 3
clear-host
try {
  $request = $null;
  #JSON
  $url1 ="https://jsonplaceholder.typicode.com/todos/1";
  #XML
  $url2 ="http://www.thomas-bayer.com/sqlrest/";
  $httpMethod = "GET"
  $request = Invoke-WebRequest $url1 -Method $httpMethod;
 
  if ($request) {
    if ($request.StatusCode -eq 200) {
      if ($request.BaseResponse.ContentType -like "application/json*") {
        Write-Host "*** JSON ***" -ForegroundColor YELLOW;
        $request.Content;
      } elseif ($request.BaseResponse.ContentType -like "application/xml*") {
        Write-Host "*** XML ***" -ForegroundColor YELLOW;
        $request.Content;
      } else {
        Write-Host "Es wurde der ContentType """ $request.BaseResponse.ContentType `
                   """ zurückgegeben (erwartet wurde: ""application/json""" `
                   "oder ""application/xml"")." -ForegroundColor RED;
      }
    } else {
      Write-Host "Es wurde der HTTP-StatusCode """ $request.StatusCode `
                 """zurückgegeben (erwartet wurde: 200)." -ForegroundColor RED;
    }
  }
}
catch {
  Write-Warning "Catch Exception"
  Write-Host "exception type :" $error[0].Exception.GetType().FullName -f YELLOW;
  Write-Host "exception reason:" $error[0].Exception.status -f YELLOW;
  Write-Host "exception text :" $error[0].Exception.Message -f YELLOW;
}

 

Einfacher Aufruf einer URL mit der Funktion „Invoke-WebRequest“ :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#REQUIRES –Version 3
Clear-Host
try {
  $request = $null;
  $url ="https://jsonplaceholder.typicode.com/todos/1";
  $httpMethod = "GET"
 
  $request = Invoke-RestMethod $url -Method $httpMethod;
  if ($request) {
      $request;
  } 
}
catch {
  Write-Warning "Catch Exception"
  Write-Host "exception type  :" $error[0].Exception.GetType().FullName -f yello
  Write-Host "exception reason:" $error[0].Exception.status -f yello
  Write-Host "exception text  :" $error[0].Exception.Message -f yello
}

 

Quellen/Weiterführende Links:
Microsoft – https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod
Fake Online REST API for Testing and Prototyping – https://jsonplaceholder.typicode.com
RestAPI Beispiele (Thomas Bayer / Predic8 GmbH) – https://www.predic8.de/rest-beispiel.htm
RestAPI Web Services erklärt (Thomas Bayer / oio.de) – https://www.oio.de/public/xml/rest-webservices.htm
RestAPI Webservices (Frank Rahn) – https://www.frank-rahn.de/restful-webservices/
RestAPI Exchange 2016 (frankysweb.de) – https://www.frankysweb.de/exchange-server-2016-rest-api/
RestAPI Security Guidelines (blog.restcase.com) – https://blog.restcase.com/top-5-rest-api-security-guidelines/