Describe the bug
The az rest fails to parse json body, as seen here:
$alert_body = Get-Content "./dynamic_threshold_metric_alert_body.json"
$uri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName_appInsights/providers/Microsoft.Insights/metricAlerts/$($ruleName)?api-version=2018-03-01"
az rest --method put --headers "Content-Type=application/json" --uri $uri --body $alert_body
with
az: error: unrecognized arguments: location: global, tags: {}, properties: {
(...)
where "./dynamic_threshold_metric_alert_body.json" is an example alert body taken from https://docs.microsoft.com/en-us/rest/api/monitor/metricalerts/createorupdate#create_or_update_a_dynamic_alert_rule_for_multiple_resources
with slight modifications to match my subscription name, resource group name, metric name, etc.
however, this works:
$alert_body = Get-Content "./dynamic_threshold_metric_alert_body.json"
$uri = "https://management.azure.com/subscriptions/$subscriptionId/resourceGroups/$resourceGroupName_appInsights/providers/Microsoft.Insights/metricAlerts/$($ruleName)?api-version=2018-03-01"
$headers = @{Authorization = "Bearer $token"; "Content-Type" = "application/json" }
Invoke-WebRequest -Method PUT -Headers $headers -Uri $uri -Body $alert_body
i.e. it returns 200 and I can observe in Azure portal that the alert got created.
To Reproduce
Run the scripts pasted above, but first set variables to appropriate values.
Expected behavior
Expected for the az rest --method put command creating an alert to return 200 and create an alert in Azure Portal.
Environment summary
azure-cli is 2.0.67, downloaded via MSI.
The file snippets come from PS1 on Windows, UTF-8 CRLF.
Additional context
Discovered while trying to work around #9640
I observed more strange json body parsing problems, like e.g. inability to handle line breaks, or properly parse json key value.
@tjprescott
@yugangw-msft for comment.
@konrad-jamrozik, please wrap the $alert_body with quotes. In case it helps, you can check out the tips.
@yugangw-msft wrapping it in quotes was not enough. I also had to do:
$alert_body = $alert_body -replace "`"", "\`""
i.e. replace all occurrences of " with \".
This fixed the issue. Thank you, and thank you for the pointer to the tips!
Closing. There is nothing further we can do
az cli should add support for -InFile, so customers can simply give a valid Json as input, rather than having to do lot of work arounds. Something in similar lines as below
Invoke-RestMethod -Method "PUT" -Headers $requestHeader -Uri $uri -InFile $replicationGroupConfigPath
az rest does support input file, please check the help with
> az rest -h
--body -b : Request body. Use @{file} to load from a file. For quoting issues
in different terminals, see https://github.com/Azure/azure-
cli/blob/dev/doc/use_cli_effectively.md#quoting-issues.
Create a public IP address from body.json file
az rest --method put --uri https://management.azure.com/subscriptions/{subscriptionId}/resou
rceGroups/{resourceGroupName}/providers/Microsoft.Network/publicIPAddresses/{publicIpAddress
Name}?api-version=2019-09-01 --body @body.json
got it thanks
Most helpful comment
@yugangw-msft wrapping it in quotes was not enough. I also had to do:
i.e. replace all occurrences of
"with\".This fixed the issue. Thank you, and thank you for the pointer to the tips!