Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 276
Nitzan Avatar asked Oct 25 '17 14:10

Nitzan


People also ask

What is the difference between invoke-RestMethod and invoke-WebRequest?

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.

How do you invoke a web request?

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.

What is invoke-RestMethod in PowerShell?

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.


1 Answers

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.

like image 122
4c74356b41 Avatar answered Oct 04 '22 04:10

4c74356b41