Invoke-WebRequest (Authentication with Cookie)
Aus Wiki-WebPerfect
Version vom 19. September 2019, 14:17 Uhr von Admin (Diskussion | Beiträge)
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