2

I'm using Powershell v4.

I am calling a POST API and want to see the response when I run Invoke-RestMethod but it just shows a new line.

enter image description here

How can I output the response from the API to the console? I have tried running it in a script with Write-Host and Write-Output, but nothing appears on the console screen.

PowerShell script:

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

I can see the response when I curl the API but cannot see it in my PowerShell script which I need to use.

CURL

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

It looks like its not even hitting the API as nothing appears in the API logs when triggering from Powershell. Not sure how to resolve that if that is the problem.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
user3165854
  • 1,505
  • 8
  • 48
  • 100
  • You should wrap your statement in a try catch block and see what Powershell is actually getting. Example: https://stackoverflow.com/questions/18771424/how-to-get-powershell-invoke-restmethod-to-return-body-of-http-500-code-response – Ty Savercool Aug 07 '17 at 17:11

1 Answers1

1

Your command has a syntax error. Your contenttype is not in proper format.

You have:

-ContentType application/json

Change it to:

-ContentType "application/json"
Collin Chaffin
  • 872
  • 9
  • 15