Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

az rest cannot parse json body #9742

Closed
konrad-jamrozik opened this issue Jun 20, 2019 · 8 comments
Closed

az rest cannot parse json body #9742

konrad-jamrozik opened this issue Jun 20, 2019 · 8 comments
Assignees
Labels
ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Resource Manager-cli Shell - PowerShell

Comments

@konrad-jamrozik
Copy link

konrad-jamrozik commented Jun 20, 2019

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

@konrad-jamrozik konrad-jamrozik changed the title az rest cannot parse body json az rest cannot parse json body Jun 20, 2019
@tjprescott
Copy link
Member

@yugangw-msft for comment.

@yugangw-msft
Copy link
Contributor

@konrad-jamrozik, please wrap the $alert_body with quotes. In case it helps, you can check out the tips.

@konrad-jamrozik
Copy link
Author

konrad-jamrozik commented Jun 20, 2019

@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!

@yugangw-msft
Copy link
Contributor

Closing. There is nothing further we can do

@mozehgir mozehgir added the ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group label Aug 14, 2019
@jiasli jiasli removed the triage label Sep 2, 2019
@haroldrandom haroldrandom added ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group needs-triage This is a new issue that needs to be triaged to the appropriate team. Resource Manager-cli labels Oct 25, 2019
@mvvsubbu
Copy link

mvvsubbu commented May 1, 2020

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

@jiasli jiasli self-assigned this May 2, 2020
@jiasli
Copy link
Member

jiasli commented May 2, 2020

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

@mvvsubbu
Copy link

mvvsubbu commented May 2, 2020

got it thanks

@jiasli
Copy link
Member

jiasli commented Apr 18, 2022

This issue is tracked by #15529.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ARM az resource/group/lock/tag/deployment/policy/managementapp/account management-group Resource Manager-cli Shell - PowerShell
Projects
None yet
Development

No branches or pull requests

7 participants