Utilizzare Invoke-RestMethod
per consumare API REST. Salvare il JSON in una stringa e l'uso che, come il corpo, es:
$JSON = @'
{"@type":"login",
"username":"[email protected]",
"password":"yyy"
}
'@
$response = Invoke-RestMethod -Uri "http://somesite.com/oneendpoint" -Method Post -Body $JSON -ContentType "application/json"
Se si utilizza PowerShell 3, so che ci sono stati alcuni problemi con Invoke-RestMethod
, ma si dovrebbe essere in grado di utilizzare Invoke-WebRequest
come sostituzione:
$response = Invoke-WebRequest -Uri "http://somesite.com/oneendpoint" -Method Post -Body $JSON -ContentType "application/json"
Se non si desidera scrivere il proprio JSON ogni volta, è possibile utilizzare una tabella hash e utilizzare PowerShell per convertirlo in JSON prima di postarlo. Ex.
$JSON = @{
"@type" = "login"
"username" = "[email protected]"
"password" = "yyy"
} | ConvertTo-Json
fonte
2016-03-01 20:23:54
Grazie mille. Questo ha aiutato !! – live2learn