我正在使用以下 CURL 命令從 API 讀取/獲取表資料:
curl -X GET \
-H 'Content-Type: application/json' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer *myAccessToken*' \
https://www.myWebsite.com/api/orders
此命令/API 呼叫回傳 JSON 格式的表。我需要做兩件事。
[1] 我需要在 powershell 中運行它。我已經嘗試使用上面的代碼,并回傳一個一般的語法錯誤:
A parameter cannot be found that matches parameter name 'X'.
[2] 在 PowerShell 中,將 JSON 輸出轉換并保存為 CSV 檔案
有任何想法嗎?謝謝!
uj5u.com熱心網友回復:
您可以使用Invoke-RestMethodCmdlet 向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 根據資料型別格式化回應。對于 JavaScript 物件表示法 (JSON) 或 XML,PowerShell 將內容轉換或反序列化為[PSCustomObject]物件。因此,您可以選擇要匯出的列并將其通過管道傳輸到Export-CsvCmdlet
$response | Select ColumnName1,ColumnName2 | Export-Csv -Path "filename.csv" -NoTypeInformation
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/359681.html
