如何让 Invoke-RestMethod 按原样打印响应正文
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.
我正在尝试使用 powershell cmdlet Invoke-RestMethod
将 json 端点打印到控制台。
命令成功执行,但最终结果是终端 table,其中包含 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...
我如何让它在不格式化的情况下将原始响应打印到终端?
首先,您提供的 -Method
和 -ContentType
是默认值,您可以删除它们。并获得 json,将其转换回 json。
Invoke-RestMethod -Uri "http://example.com/" | ConvertTo-Json -Depth 10
我不知道你为什么要转换回 json,但是好吧,给你。
另一个答案非常适合 JSON 响应,但 -UseBasicParsing 开关适用于任何响应类型:
(Invoke-WebRequest -UseBasicParsing -Uri 'http://example.com').Content
只会给你响应内容(在 example.com 情况下,原始 html,或者在你的用例中,原始 JSON)。
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.
我正在尝试使用 powershell cmdlet Invoke-RestMethod
将 json 端点打印到控制台。
命令成功执行,但最终结果是终端 table,其中包含 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...
我如何让它在不格式化的情况下将原始响应打印到终端?
首先,您提供的 -Method
和 -ContentType
是默认值,您可以删除它们。并获得 json,将其转换回 json。
Invoke-RestMethod -Uri "http://example.com/" | ConvertTo-Json -Depth 10
我不知道你为什么要转换回 json,但是好吧,给你。
另一个答案非常适合 JSON 响应,但 -UseBasicParsing 开关适用于任何响应类型:
(Invoke-WebRequest -UseBasicParsing -Uri 'http://example.com').Content
只会给你响应内容(在 example.com 情况下,原始 html,或者在你的用例中,原始 JSON)。