question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
SatishBoddu-MSFTcommented, May 31, 2022

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

0reactions
msftbot[bot]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
Read more comments on GitHub >

github_iconTop Results From Across the Web

Tips for using the Azure CLI successfully - Microsoft Learn
Learn tips for using Azure CLI successfully, such as output formats, passing parameter values, and quoting rules for different shells.
Read more >
API Reference - Librato
Each measurement has a value that represents an observation at one point in time. The value of a measurement will typically vary between...
Read more >
Oracle Database Release 19c New Features
You can use JSON_SERIALIZE to extract JSON values as text for printing or display. ... Oracle Data Guard has several processes, on the...
Read more >
Formulas—ArcGIS Survey123 | Documentation
This function is used in time and dateTime questions. It behaves the same as today() in date questions. now(). once(). If a question...
Read more >
OpenAPI Specification - Version 2.0 - Swagger
(fka Swagger RESTful API Documentation Specification) ... For example, if a field is said to have an array value, the JSON array representation...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found