[英]Using GET in PowerShell & Exporting JSON to CSV
I'm using the following CURL command, to read/fetch table data from an API:我正在使用以下 CURL 命令从 API 读取/获取表数据:
curl -X GET \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer *myAccessToken*' \
https://www.myWebsite.com/api/orders
This command/API Call returns a table in JSON format.此命令/API 调用返回 JSON 格式的表。 I need to do two things with this.
我需要做两件事。
[1] I need to run this in powershell. [1] 我需要在 powershell 中运行它。 I've tried using the above code, and returns a general syntax error:
我已经尝试使用上面的代码,并返回一个一般的语法错误:
A parameter cannot be found that matches parameter name 'X'.
[2] In PowerShell, Have the JSON output converted & saved as a CSV file [2] 在 PowerShell 中,将 JSON 输出转换并保存为 CSV 文件
Any ideas?有任何想法吗? Thanks!
谢谢!
You can use Invoke-RestMethod
Cmdlet to Sends an HTTP or HTTPS request to a RESTful web service.您可以使用
Invoke-RestMethod
Cmdlet 向RESTful Web 服务发送HTTP 或 HTTPS 请求。
$uri = "https://www.myWebsite.com/api/orders"
$headers = @{
'Content-Type' = 'application/json'
'Authorization' = 'Bearer <AccessToken>'
'Accept'= 'application/json'
}
$response = Invoke-RestMethod -Uri $uri -Method GET -Headers $headers
PowerShell formats the response based to the data type . PowerShell 根据数据类型格式化响应。 For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into
[PSCustomObject]
objects.对于 JavaScript 对象表示法 (JSON) 或 XML,PowerShell 将内容转换或反序列化为
[PSCustomObject]
对象。 So you can select the columns you want to export and pipe it into Export-Csv
Cmdlet因此,您可以选择要导出的列并将其通过管道
Export-Csv
到Export-Csv
Cmdlet
$response | Select ColumnName1,ColumnName2 | Export-Csv -Path "filename.csv" -NoTypeInformation
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.