调用 Azure REST API 时获取 'Bad Request' 和 'Invalid query definition'
Getting 'Bad Request' and 'Invalid query definition' when calling Azure REST API
我正在尝试调用 Azure REST API 端点来获取我的日常使用成本信息。
我使用的是 Powershell 7.2,这是我的代码:
$uri = 'https://management.azure.com/subscriptions/{subscription id}/providers/Microsoft.CostManagement/query?api-version=2021-10-01'
$token = '{generated bearer token string}'
$securetoken = ConvertTo-SecureString $token -AsPlainText -Force
$body = @{
type = 'Usage'
timeframe = 'MonthToDate'
dataset = @{
granularity = 'Daily'
aggregation = @{
totalCost = @{
name = 'PreTaxCost'
function = 'Sum'
}
}
grouping = @(
@{
type = 'Dimension'
name = 'ServiceName'
}
)
}
}
$costresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication Bearer -Token $securetoken -Body $body
Write-Host $costresponse
这是我尝试模拟的 Microsoft 文档中的请求正文示例:
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
当我 运行 代码时,我收到此错误消息:
Line |
27 | … tresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"error":{"code":"BadRequest","message":"Invalid query definition: Missing dataset granularity; valid
| values: 'Daily'.\r\n\r\n (Request ID: c6ead005-85b3-4ebe-9b46-........)"}}
我认为错误与正文语法有关,但我无法弄清楚问题出在哪里。我正在关注此 Microsoft 文档:https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage
编辑:
我尝试将主体转换为 JSON 并添加如下深度参数:
$costresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication Bearer -Token $securetoken -Body ($body|ConvertTo-Json -Depth 5)
现在我收到一条略有不同的错误消息:
Line |
26 | … tresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"error":{"code":"BadRequest","message":"Invalid query definition, Dataset is invalid or not supplied.
| (Request ID: cf6a4b8f-88e8-4037-aa33-904......)"}}
我需要将 -ContentType 'application/json'
添加到我的 Invoke-WebRequest 函数中才能使其正常工作。感谢@bluuf 的建议
我正在尝试调用 Azure REST API 端点来获取我的日常使用成本信息。
我使用的是 Powershell 7.2,这是我的代码:
$uri = 'https://management.azure.com/subscriptions/{subscription id}/providers/Microsoft.CostManagement/query?api-version=2021-10-01'
$token = '{generated bearer token string}'
$securetoken = ConvertTo-SecureString $token -AsPlainText -Force
$body = @{
type = 'Usage'
timeframe = 'MonthToDate'
dataset = @{
granularity = 'Daily'
aggregation = @{
totalCost = @{
name = 'PreTaxCost'
function = 'Sum'
}
}
grouping = @(
@{
type = 'Dimension'
name = 'ServiceName'
}
)
}
}
$costresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication Bearer -Token $securetoken -Body $body
Write-Host $costresponse
这是我尝试模拟的 Microsoft 文档中的请求正文示例:
{
"type": "Usage",
"timeframe": "TheLastMonth",
"dataset": {
"granularity": "None",
"aggregation": {
"totalCost": {
"name": "PreTaxCost",
"function": "Sum"
}
},
"grouping": [
{
"type": "Dimension",
"name": "ResourceGroup"
}
]
}
}
当我 运行 代码时,我收到此错误消息:
Line |
27 | … tresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"error":{"code":"BadRequest","message":"Invalid query definition: Missing dataset granularity; valid
| values: 'Daily'.\r\n\r\n (Request ID: c6ead005-85b3-4ebe-9b46-........)"}}
我认为错误与正文语法有关,但我无法弄清楚问题出在哪里。我正在关注此 Microsoft 文档:https://docs.microsoft.com/en-us/rest/api/cost-management/query/usage
编辑: 我尝试将主体转换为 JSON 并添加如下深度参数:
$costresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication Bearer -Token $securetoken -Body ($body|ConvertTo-Json -Depth 5)
现在我收到一条略有不同的错误消息:
Line |
26 | … tresponse = Invoke-WebRequest -Uri $uri -Method Post -Authentication …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"error":{"code":"BadRequest","message":"Invalid query definition, Dataset is invalid or not supplied.
| (Request ID: cf6a4b8f-88e8-4037-aa33-904......)"}}
我需要将 -ContentType 'application/json'
添加到我的 Invoke-WebRequest 函数中才能使其正常工作。感谢@bluuf 的建议