简体   繁体   English

如何从调用Invoke-WebRequest访问JSON?

[英]How do I access the JSON from calling Invoke-WebRequest?

I need to call an API (to get a list of records from my DNS provider), and that can be done using curl. 我需要调用API(从我的DNS提供程序获取记录列表),这可以使用curl完成。 This returns formatted json with all of my records 这将返回带有所有记录的格式化的json

curl -H 'Authorization: Bearer mytoken' -H 'Accept: application/json' https://api.dnsimple.com/v2/12345/zones/example.com/records

However, I need to be able to do this from PowerShell 但是,我需要能够从PowerShell执行此操作

$uri = "https://api.dnsimple.com/v2/12345/zones/example.com/records"
$headers = @{}
$headers["Authorization"] = "Bearer mytoken"
$headers["Accept"] = "application/json"
$foo = Invoke-WebRequest $uri -Headers $headers 

This command runs, but where in $foo can I access the returned JSON? 这个命令运行,但在$ foo中我可以访问返回的JSON吗?

With Invoke-WebRequest , you would access the Content property: $foo.Content 使用Invoke-WebRequest ,您将访问Content属性: $foo.Content

Note that you can also use Invoke-RestMethod , which automatically converts JSON responses to PowerShell objects. 请注意,您还可以使用Invoke-RestMethod ,它会自动将JSON响应转换为PowerShell对象。

So this: 所以这:

$o = Invoke-RestMethod #params

Would be the same as this: 会是这样的:

$foo = Invoke-WebRequest #params
$o = $foo.Content | ConvertFrom-Json

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解析/访问 Powershell 4 中 Invoke-WebRequest 返回的 JSON - How to parse/access JSON returned by Invoke-WebRequest in Powershell 4 如何从 PowerShell 中的 Invoke-WebRequest 解析 JSON? - How to parse JSON from the Invoke-WebRequest in PowerShell? Powershell:使用 Invoke-WebRequest,如何在 JSON 中使用变量? - Powershell: Using Invoke-WebRequest, How To Use Variable Within JSON? 从CSV文件读取数据并将一盒数据转换为JSON Invoke-WebRequest - Read data from CSV file and convert one box of data to JSON Invoke-WebRequest Powershell Invoke-Webrequest w/ JSON Body - 无法反序列化……? - Powershell Invoke-Webrequest w/ JSON Body - Can not deserialize…? 将 cURL 转换为 PowerShell Invoke-WebRequest - JSON 数据表问题 - Convert cURL to PowerShell Invoke-WebRequest - JSON data table issue Invoke-Webrequest 不正确的 Json 格式“无法绑定参数 'Headers'” - Invoke-Webrequest incorect Json format "Cannot bind parameter 'Headers' " Invoke-WebRequest失败-使用GitHub API的“问题解析JSON” - Invoke-WebRequest failed - “Problems parsing json” with GitHub api 我如何显示在System.Object [] Powershell内部,invoke-webrequest - How can I show inside of System.Object[] Powershell, invoke-webrequest 如何为具有多个来自CSV变量的API调用设计“ Invoke-WebRequest”? (电源外壳) - How to design an “Invoke-WebRequest” for multiple API calls with more than one variable from CSV? (Powershell)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM