Invoke-WebRequest (Authentication with Cookie): Unterschied zwischen den Versionen

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „''This is a example of PowerShell REST-Call to get a Web-Cookie and authenticate with the Cookie.'' == Get the Web-Cookie from API == <source lang="powershell…“)
 
(kein Unterschied)

Aktuelle Version vom 19. September 2019, 14:17 Uhr

This is a example of PowerShell REST-Call to get a Web-Cookie and authenticate with the Cookie.

Get the Web-Cookie from API

$APIAuth = "http://<Your-Authentication-URL>"
 
$Body = [PSCustomObject]@{
    UserName = "<UserName>"
    password = "<Password>"
} | ConvertTo-Json
 
Invoke-WebRequest -Uri $APIAuth -Body $Body -Method Post -ContentType application/json -UseBasicParsing -SessionVariable Cookie

Now you have a variable named $Cookie with the Web-Cookie inside.


Authenticate with this Cookie

Now you can use this variable $Cookie to authenticate to your API.

$APIURL = "http://<Your-API-URL>"
 
$Body = [PSCustomObject]@{
    <Key> = "<Value>"
} | ConvertTo-Json
 
$Result = Invoke-WebRequest -Uri $APIURL -Body $Body -Method Post -ContentType application/json -UseBasicParsing -WebSession $Cookie