[英]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.