jsonrestpowershell-5.0

How do I make Invoke-RestMethod print the response body as is


Note: this question is here for historical reasons or for users of Powershell < 6.0.0. This problem should not occur in Powershell 6 and beyond.

I'm trying to use the powershell cmdlet Invoke-RestMethod to print a json endpoint to the console.

The command executes successfully but the end result is a terminal table containing all of the json's level 1 fields.

I'm running the cmdlet like this:

Invoke-RestMethod -Uri <some url here> -Method GET -ContentType "application/json"

And getting something like this:

sections _links
-------- ------
         {@{rel=parent; href=https://us9.api.mailchimp.com/3.0/templates/138...

How do I get it to just print the raw response to the terminal without formatting?


Solution

  • First of all, the -Method and -ContentType you are providing are the default, you can drop those. And to get the json, cast it back to json.

    Invoke-RestMethod -Uri "http://example.com/" | ConvertTo-Json -Depth 10
    

    I have no idea why you want to cast back to json, but well, here you go.