2016-03-01 34 views

risposta

14

Si dovrebbe essere in grado di effettuare le seguenti operazioni:

$params = @{"@type"="login"; 
"username"="[email protected]"; 
"password"="yyy"; 
} 

Invoke-WebRequest -Uri http://foobar.com/endpoint -Method POST -Body $params 

Questo sarà inserire il corpo come specificato.

+1

Grazie mille. Questo ha aiutato !! – live2learn

23

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 
+0

Grazie mille! Questo ha aiutato !! – live2learn