Sử dụng Invoke-RestMethod
để sử dụng REST-API. Save the JSON thành một chuỗi và sử dụng như cơ thể, ví dụ:
$JSON = @'
{"@type":"login",
"username":"[email protected]",
"password":"yyy"
}
'@
$response = Invoke-RestMethod -Uri "http://somesite.com/oneendpoint" -Method Post -Body $JSON -ContentType "application/json"
Nếu bạn sử dụng Powershell 3, tôi biết đã có một số vấn đề với Invoke-RestMethod
, nhưng bạn sẽ có thể sử dụng Invoke-WebRequest
như một sự thay thế:
$response = Invoke-WebRequest -Uri "http://somesite.com/oneendpoint" -Method Post -Body $JSON -ContentType "application/json"
Nếu bạn không muốn viết JSON của riêng mình mỗi lần, bạn có thể sử dụng thẻ bắt buộc và sử dụng PowerShell để chuyển đổi sang JSON trước khi đăng. Ví dụ
$JSON = @{
"@type" = "login"
"username" = "[email protected]"
"password" = "yyy"
} | ConvertTo-Json
Nguồn
2016-03-01 20:23:54
Cảm ơn rất nhiều. Điều này đã giúp !! – live2learn