如何使Invoke-RestMethod按原样打印响应主体

问题描述 投票:6回答:2

注意:此问题出于历史原因或Powershell <6.0.0的用户。 Powershell 6及更高版本中不应出现此问题。

我正在尝试使用powershell cmdlet Invoke-RestMethod将json端点打印到控制台。

该命令成功执行,但最终结果是包含所有json的1级字段的终端表。

我正在运行这样的cmdlet:

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

得到这样的东西:

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

如何在不格式化的情况下将原始响应打印到终端?

json rest powershell-v5.0
2个回答
7
投票

首先,你提供的-Method-ContentType是默认值,你可以删除它们。为了得到json,把它扔回json。

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

我不知道你为什么要回到json,但好吧,你走了。


3
投票

另一个答案适用于JSON响应,但-UseBasicParsing开关适用于任何响应类型:

(Invoke-WebRequest -UseBasicParsing -Uri 'http://example.com').Content

将仅提供响应内容(在example.com案例中,原始html,或在您的用例中,原始JSON)。

© www.soinside.com 2019 - 2024. All rights reserved.