Invoke-WebRequest (Authentication with Cookie)

Aus Wiki-WebPerfect
Wechseln zu: Navigation, Suche

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