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?
Invoke-RestMethod - unlike Invoke-WebRequest - has deserialization built in: with a JSON response, it automatically parses the JSON text returned into a [pscustomobject] graph as if ConvertFrom-Json had been applied to it.
The Invoke-WebRequest cmdlet sends HTTP, HTTPS, FTP, and FILE requests to a web page or web service. It parses the response and returns collections of forms, links, images, and other significant HTML elements. This cmdlet was introduced in Windows PowerShell 3.0.
Description. The Invoke-RestMethod cmdlet sends HTTP and HTTPS requests to Representational State Transfer (REST) web services that return richly structured data. PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With