az rest breaks when JSON parameter value has a space in it
- Dominant language
- Python
- Stars
- 4.6k
- Forks
- 3.5k
- Avg merge
- 3d 2h
- Merged PRs (30d)
- 60
Description
**Related command**
az rest
**Describe the bug**
passing az rest a json body object with a property that has a space in the value throws ERROR: unrecognized arguments:
**To Reproduce**
Whenever any of the string variables below contain an empty space, the error will be thrown. For example, when the triggerPipeRequestedFor variable equals "John Doe", the error will be thrown, but if I manually set the value replacing the space with an underscore "John_Doe" the az rest request succeeds.
```
- task: AzureCLI@2
displayName: Call Azure Function
inputs:
azureSubscription: 'OurSubscription'
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
$functionName = "${{ parameters.functionName }}"
echo functionName: $functionName
$functionKeys = az functionapp function keys list -g our-resource-group -n ourazurefunction --function-name $functionName | ConvertFrom-Json
$defaultKey = $functionKeys.default
$url = "https://ourazurefunction.azurewebsites.net/api/$($functionName)?code=$($defaultKey)"
echo url: $url
$body = @"
{
"BuildInitiatorName": "$(triggerPipeRequestedFor)",
"BuildInitiatorId": "$(triggerPipeRequestedForID)",
"Stage": "$(System.StageName)",
"EnvironmentId": $(Environment.Id),
"EnvironmentName": "$(Environment.Name)",
"ResourceId": $(Environment.ResourceId),
"ResourceName": "$(Environment.ResourceName)",
"BuildPipelineId": $(triggerPipeID),
"BuildPipelineName": "$(triggerPipeName)",
"BuildPipelineRunId": $(triggerPipeRunID),
"BuildPipelineRunName": "$(triggerPipeRunName)",
"DeploymentPipelineId": $(system.definitionId),
"DeploymentPipelineName": "$(pipelineIdentifier)",
"DeploymentPipelineRunId": $(Build.BuildId),
"FailureReason": "$(deploymentFailureReason)"
}
"@
echo body: $body
$body = ($body | ConvertTo-Json -Compress).Replace('\n', '')
echo bodyCompressed: $body
az rest --method post --uri $url --skip-authorization-header --verbose --body $body
```
**Expected behavior**
Expect to be able to pass a typical json body to az rest and have the request complete.
**Environment summary**
The AzureCLI@2 task is being executed on a Windows Server 2019 VM in Azure.
azure-cli 2.34.1 *
core 2.34.1 *
telemetry 1.0.6
Dependencies:
msal 1.16.0
azure-mgmt-resource 20.0.0
**Additional context**
`echo body: $body` in the powershell snippet above writes the following
```
body:
{
"BuildInitiatorName": "John Doe",
"BuildInitiatorId": "a2a57432-aa5a-4aa1-aa06-c2aa53957b7a",
"Stage": "Dev_Stage",
"EnvironmentId": 7,
"EnvironmentName": "Dev",
"ResourceId": 3,
"ResourceName": "DEV-VM",
"BuildPipelineId": 109,
"BuildPipelineName": "API-Build",
"BuildPipelineRunId": 66715,
"BuildPipelineRunName": "main_2022.3.0.66715",
"DeploymentPipelineId": 114,
"DeploymentPipelineName": "API-Deployment",
"DeploymentPipelineRunId": 66744,
"FailureReason": ""
}
```
`echo bodyCompressed: $body` in the powershell directly before calling az rest writes the following
```
bodyCompressed:
"{\"BuildInitiatorName\": \"John Doe\",\"BuildInitiatorId\": \"a2a57432-aa5a-4aa1-aa06-c2aa53957b7a\",\"Stage\": \"Dev_Stage\",\"EnvironmentId\": 7,\"EnvironmentName\": \"Dev\",\"ResourceId\": 3,\"ResourceName\": \"DEV-VM\",\"BuildPipelineId\": 109,\"BuildPipelineName\": \"API-Build\",\"BuildPipelineRunId\": 66715,\"BuildPipelineRunName\": \"main_2022.3.0.66715\",\"DeploymentPipelineId\": 114,\"DeploymentPipelineName\": \"API-Deployment\",\"DeploymentPipelineRunId\": 66744,\"FailureReason\": \"\"}"
```
To me that looks like json that one would expect to succeed being passed as body content in a rest request, though typically without the escape \ characters.
The error thrown in the logs looks like
```
ERROR: unrecognized arguments: "John Doe","BuildInitiatorId": "a2a57432-aa5a-4aa1-aa06-c2aa53957b7a","Stage": "Dev_Stage","EnvironmentId": 7,"EnvironmentName": "Dev","ResourceId": 3,"ResourceName": "DEV-VM","BuildPipelineId": 109,"BuildPipelineName": "API-Build","BuildPipelineRunId": 66715,"BuildPipelineRunName": "main_2022.3.0.66715","DeploymentPipelineId": 114,"DeploymentPipelineName": "API-Deployment","DeploymentPipelineRunId": 66744,"FailureReason": ""}
```
Notice that it appears to remove the first part of the object; the opening { and first property name. Also the position of the property which contains a space in its value doesn't seem to matter. Originally, the BuildInitiatorName property was near the bottom of the list, but the error message looked the same with the opening { and first property name missing.
Remove the BuildInitiatorName property and the request completes successfully. Populate BuildInitiatorName value with "John_Doe" and the request completes successfully. Populate BuildInitiatorName value with "John Doe" and the request fails.
I'm new to Azure CLI and while I was excited when I finally got the call to our Azure Function working without needing to access key vault or putting access credentials in our code repository, it was a struggle creating this json body in powershell in a way that would work. But it did work awesomely. Then I needed to add the BuildInitiatorName into the request and it started failing with the error described herein and I have been beating my head against numerous Google and Bing searches trying to find the right question to ask to be able to find a way to make this work, but with no success. Ultimately, one of those searches brought me to your GitHub repo and I'm hoping you can help. Either this is a bug or else it must be doable.
Thank you for any assistance you can give.
Contributor guide
Assessment
This issue has not been assessed yet.