2017-08-07 81 views
1

我正在使用Powershell v4。在Powershell中未顯示API響應

我打電話給一個POST API並希望看到響應,當我運行Invoke-RestMethod但它只是顯示一個新行。

enter image description here

我怎麼能輸出從API到控制檯的響應?我曾嘗試在Write-HostWrite-Output的腳本中運行它,但控制檯屏幕上沒有顯示任何內容。

PowerShell腳本:

$Response = Invoke-RestMethod -Uri https://localhost:8081/myAPI -Body '{"username":"xyz","password":"xyz"}' -ContentType application/json -Method POST 
Write-Host $Response 

我能看到的響應,當我curl的API,但不能看到它在我的PowerShell腳本,我需要使用。

捲曲

curl -H "Content-Type: application/json" -X POST -d '{"username":"xyz","password":"xyz"}' https://localhost:8081/myAPI 

它看起來像它甚至沒有擊中API如無物從Powershell的觸發時出現在API日誌。不知道如何解決,如果這是問題。

+0

你應該換你的語句在一個try catch塊,看看PowerShell是實際上得到。示例:https://stackoverflow.com/questions/18771424/how-to-get-powershell-invoke-restmethod-to-return-body-of-http-500-code-response –

回答

0

您的命令有語法錯誤。您的內容類型格式不正確。

您有:

-ContentType application/json 

將其更改爲:

-ContentType "application/json"