2016-03-01 16 views

Trả lời

14

Bạn sẽ có thể làm như sau:

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

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

này sẽ đăng cơ thể như bạn chỉ định.

+1

Cảm ơn rất nhiều. Điều này đã giúp !! – live2learn

23

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

Cảm ơn rất nhiều! Điều này đã giúp !! – live2learn

Các vấn đề liên quan