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 breaks when JSON parameter value has a space in it #22550

Open
cResults opened this issue May 20, 2022 · 7 comments
Open

az rest breaks when JSON parameter value has a space in it #22550

cResults opened this issue May 20, 2022 · 7 comments
Labels
Auto-Assign Auto assign by bot customer-reported Issues that are reported by GitHub users external to the Azure organization. Functions az functionapp needs-team-attention This issue needs attention from Azure service team or SDK team Service Attention This issue is responsible by Azure service team.
Milestone

Comments

@cResults
Copy link

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.

@ghost ghost added customer-reported Issues that are reported by GitHub users external to the Azure organization. Auto-Assign Auto assign by bot CXP Attention This issue is handled by CXP team. Functions az functionapp labels May 20, 2022
@ghost ghost assigned jiasli May 20, 2022
@ghost ghost added this to the Backlog milestone May 20, 2022
@yonzhan
Copy link
Collaborator

yonzhan commented May 20, 2022

route to CXP team

@SatishBoddu-MSFT SatishBoddu-MSFT self-assigned this May 23, 2022
@SatishBoddu-MSFT
Copy link

@cResults
Thanks for the feedback! We are currently investigating and will update you shortly.

@SatishBoddu-MSFT
Copy link

Hello @cResults Could you please confirm if you are using the quotes for the value "BuildInitiatorName"? Wanted to check if adding quotes to the value resolve this issue/

@SatishBoddu-MSFT SatishBoddu-MSFT added the needs-author-feedback More information is needed from author to address the issue. label May 23, 2022
@cResults
Copy link
Author

cResults commented May 23, 2022

@SatishBoddu-MSFT Thank you for giving this prompt attention. I'm not sure what you are asking. The code as recorded above is failing. Is there some place in the code block of the "To Reproduce" section that is missing quotes? As far as I can tell from printing out the body variable right before calling az rest, everything I'd expect to be escaped and quoted including the BuildInitiatorName's value is escaped and quoted?

Please let me know if i answered your question. I appreciate the dialog and will try any changes you recommend.

@ghost ghost added needs-team-attention This issue needs attention from Azure service team or SDK team and removed needs-author-feedback More information is needed from author to address the issue. labels May 23, 2022
@SatishBoddu-MSFT SatishBoddu-MSFT removed their assignment May 31, 2022
@SatishBoddu-MSFT
Copy link

Hello @cResults Thanks for sharing more information on this, we are routing this to the Service Teams Attention!

@SatishBoddu-MSFT SatishBoddu-MSFT added Service Attention This issue is responsible by Azure service team. and removed CXP Attention This issue is handled by CXP team. labels May 31, 2022
@ghost
Copy link

ghost commented May 31, 2022

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Stefanus Hinardi, @Francisco-Gamino.

Issue Details

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.

Author: cResults
Assignees: -
Labels:

Service Attention, Functions, customer-reported, needs-team-attention, Auto-Assign

Milestone: Backlog

@jscheeringa
Copy link

jscheeringa commented May 18, 2023

Was there ever a resolution to this (using az cli 2.48.1)? I am seeing a similiar issue when posting json. I suspect it is happening when the json is more complex but having an issue verifying.

When I post json (simple example) :

$body = {\"param1\":\"8bdf1111-a03e-4dd4-abba-a2577dabebb6\",\"param2\":\"value with some spaces\",\"Data\":{\"paramd1\":\"https://www.someurl.com\",\"paramd2\":\"data with spaces\"},\"AppData\":{\"paramad1\":\"https://www.someurl.com\",\"paramad2\":\"data with spaces\"}}

# then post via az rest
AZ rest --method POST -u $url --body $body --resource $resource --headers "Content-type=application/json" --verbose --debug

What is interesting is that if --debug is set, you will see that the az rest body being submitted has replaced my spaces with ' ,' in the debug body output.

image

--update and for anyone else seeing this. Write your json to a file and post it that way (--body @c:\path\to\file.json). this avoids these issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Assign Auto assign by bot customer-reported Issues that are reported by GitHub users external to the Azure organization. Functions az functionapp needs-team-attention This issue needs attention from Azure service team or SDK team Service Attention This issue is responsible by Azure service team.
Projects
None yet
Development

No branches or pull requests

5 participants