azure-cli
azure-cli copied to clipboard
"ERROR: Unsupported Media Type" when trying to use `az rest` via Azure DevOps pipelines
Describe the bug
We have been using az rest
calls to set the HTTP 2.0 proxy flag parameter for an app service via AzDO pipelines, the last good run was on Jan 16 using Azure CLI version 2.56.0. Recently we attempted to run our pipeline and encountered the following error:
ERROR: Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '<null>' is not supported. Only 'application/json' is supported."}})
There have been no changes to the pipeline or the script that runs to set this parameter, the only difference I could see was that the Azure CLI version in the pipeline has been updated to 2.57.0. The script in question is a PowerShell script with a single line that executes the az rest
command below.
Related command
az rest --method PUT --uri "https://management.azure.com/subscriptions/$Subscription/resourceGroups/$ResourceGroup/providers/Microsoft.Web/sites/$AppName/config/web?api-version=2022-03-01" --body '{"properties":{"http20ProxyFlag":2}}'
Errors
ERROR: Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '
Issue script & Debug output
DEBUG: cli.knack.cli: Command arguments: ['rest', '--method', 'PUT', '--uri', 'https://management.azure.com/subscriptions/[[REDACTED]]/resourceGroups/[[REDACTED]]/providers/Microsoft.Web/sites/[[REDACTED]]/config/web?api-version=2022-03-01', '--body', '{\"properties\":{\"http20ProxyFlag\":2}}', '--debug']
DEBUG: cli.knack.cli: init debug log:
Cannot enable color.
DEBUG: cli.knack.cli: Event: Cli.PreExecute []
DEBUG: cli.knack.cli: Event: CommandParser.OnGlobalArgumentsCreate [<function CLILogging.on_global_arguments at 0x7f1a08e180e0>, <function OutputProducer.on_global_arguments at 0x7f1a08dba200>, <function CLIQuery.on_global_arguments at 0x7f1a08defce0>]
DEBUG: cli.knack.cli: Event: CommandInvoker.OnPreCommandTableCreate []
DEBUG: cli.azure.cli.core: Modules found from index for 'rest': ['azure.cli.command_modules.util']
DEBUG: cli.azure.cli.core: Loading command modules:
DEBUG: cli.azure.cli.core: Name Load Time Groups Commands
DEBUG: cli.azure.cli.core: util 0.001 3 7
DEBUG: cli.azure.cli.core: Total (1) 0.001 3 7
DEBUG: cli.azure.cli.core: These extensions are not installed and will be skipped: ['azext_ai_examples', 'azext_next']
DEBUG: cli.azure.cli.core: Loading extensions:
DEBUG: cli.azure.cli.core: Name Load Time Groups Commands Directory
DEBUG: cli.azure.cli.core: Total (0) 0.000 0 0
DEBUG: cli.azure.cli.core: Loaded 3 groups, 7 commands.
DEBUG: cli.azure.cli.core: Found a match in the command table.
DEBUG: cli.azure.cli.core: Raw command : rest
DEBUG: cli.azure.cli.core: Command table: rest
DEBUG: cli.knack.cli: Event: CommandInvoker.OnPreCommandTableTruncate [<function AzCliLogging.init_command_file_logging at 0x7f1a07cc9940>]
DEBUG: cli.azure.cli.core.azlogging: metadata file logging enabled - writing logs to '/home/vsts/work/_temp/.azclitask/commands/2024-03-01.20-25-00.rest.1818.log'.
INFO: az_command_data_logger: command args: rest --method {} --uri {} --body {} --debug
DEBUG: cli.knack.cli: Event: CommandInvoker.OnPreArgumentLoad [<function register_global_subscription_argument.
ERROR: cli.azure.cli.core.azclierror: Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/opt/az/lib/python3.11/site-packages/azure/cli/core/decorators.py", line 79, in _wrapped_func return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/telemetry.py", line 532, in _get_secrets_warning_config show_secrets_warning = _get_config().getboolean('clients', 'show_secrets_warning', fallback=None) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/knack/config.py", line 147, in getboolean raise ValueError('Not a boolean: {}'.format(val)) ValueError: Not a boolean: None
INFO: cli.azure.cli.core.decorators: Suppress exception:
Traceback (most recent call last):
File "/opt/az/lib/python3.11/site-packages/azure/cli/main.py", line 62, in
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "/opt/az/lib/python3.11/site-packages/azure/cli/core/decorators.py", line 79, in _wrapped_func return func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/telemetry.py", line 124, in generate_payload payload = json.dumps(self.events, separators=(',', ':')) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/json/init.py", line 238, in dumps **kw).encode(obj) ^^^^^^^^^^^ File "/opt/az/lib/python3.11/json/encoder.py", line 200, in encode chunks = self.iterencode(o, _one_shot=True) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/json/encoder.py", line 258, in iterencode return _iterencode(o, 0) ^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/json/encoder.py", line 180, in default raise TypeError(f'Object of type {o.class.name} ' TypeError: Object of type HTTPError is not JSON serializable
INFO: telemetry.main: Split cli events and extra events failure: the JSON object must be str, bytes or bytearray, not NoneType
Expected behavior
Expected behavior is that the Content-Type
header is set by default to application/json
as per the az rest documentation here:
https://learn.microsoft.com/en-us/cli/azure/reference-index?view=azure-cli-latest#az-rest
Then that the HTTP 2.0 Proxy flag is set correctly and that the response from az rest
returns the updated state of the resource rather than an error.
Environment Summary
azure-cli 2.57.0
core 2.57.0 telemetry 1.1.0
Extensions: azure-devops 0.26.0
Dependencies: msal 1.26.0 azure-mgmt-resource 23.1.0b2
Python location '/opt/az/bin/python3' Extensions directory '/opt/az/azcliextensions'
Python (Linux) 3.11.7 (main, Jan 31 2024, 05:29:49) [GCC 11.4.0]
Additional context
No response
Hi @ehornby Find similar issue https://github.com/Azure/azure-cli/issues/10143.
Issue title | Unsupported Media Type |
Create time | 2019-08-04 |
Comment number | 5 |
Please confirm if this resolves your issue.
Thank you for opening this issue, we will look into it.
Hi @ehornby Find similar issue #10143.
Issue title Unsupported Media Type Create time 2019-08-04 Comment number 5 Please confirm if this resolves your issue.
I did see this issue but unfortunately it does not help with the resolution of what I'm currently encountering as my JSON body is a single line string with double quotes escaped as expected.
I edited your issue description to surround code with backticks, as markdown renders \"
as "
.
You are passing '{\"properties\":{\"http20ProxyFlag\":2}}'
which is correct according to https://github.com/Azure/azure-cli/blob/dev/doc/quoting-issues-with-powershell.md (https://github.com/Azure/azure-cli/issues/15529).
However, what Azure CLI receives is
DEBUG: cli.knack.cli: Command arguments: [... '--body', '{\\"properties\\":{\\"http20ProxyFlag\\":2}}', '--debug']
I doubt the request body you passed to Azure CLI is not actually '{\"properties\":{\"http20ProxyFlag\":2}}'
, but '{\\\"properties\\\":{\\\"http20ProxyFlag\\\":2}}'
, as this produces the same debug output in PowerShell 7.4.1.
> az --debug '{\\\"properties\\\":{\\\"http20ProxyFlag\\\":2}}'
cli.knack.cli: Command arguments: ['--debug', '{\\"properties\\":{\\"http20ProxyFlag\\":2}}']
Could you doubt confirm that?
Ops, I just noticed the debug log shows Azure CLI is from /opt/az/lib/python3.11
which is a Linux path, so you have changed your shell to Bash. You should just use '{"properties":{"http20ProxyFlag":2}}'
in Bash. No twice-escaping is needed.
$ az --debug '{"properties":{"http20ProxyFlag":2}}'
cli.knack.cli: Command arguments: ['--debug', '{"properties":{"http20ProxyFlag":2}}']
$ az --debug '{\"properties\":{\"http20ProxyFlag\":2}}'
cli.knack.cli: Command arguments: ['--debug', '{\\"properties\\":{\\"http20ProxyFlag\\":2}}']
Hi @jiasli,
I tried executing the script without the escaping as suggested in your second reply and appeared to be successful - thank you!
I'm a little confused however as to why this would be executing in bash - the script file is saved as Powershell and is running inside an AzureCLI@2 task in a YAML pipeline with scriptType set to pscore
. We are using a Linux agent to execute this stage of the pipeline but my understanding was that it should be using Powershell Core to run the script, not bash. Is the Azure CLI using bash internally to invoke PS Core?
- task: AzureCLI@2
displayName: Set HTTP 2.0 Proxy flag in WestUS2
inputs:
azureSubscription: [[REDACTED]]
scriptType: 'pscore'
scriptLocation: 'scriptPath'
scriptPath: '$(terraformScriptsArtifact)/EnableHttp20Proxy.ps1
Also don't fully understand why this was working as intended prior to the version bump to 2.57.0 of Azure CLI, perhaps that's not actually related to the error I was experiencing but I can't identify anything else that has changed in the meantime.
Is the Azure CLI using bash internally to invoke PS Core?
Azure CLI is not invoking Bash. It is Bash/PowerShell that invokes Azure CLI. It may be because pscore
behaves differently on Windows and Linux agents.
As for the details regarding pscore
and its command parsing logic, I would suggest contacting Azure DevOps or PowerShell directly.
why this was working as intended prior to the version bump to 2.57.0 of Azure CLI
We didn't change anything regarding the entry script in 2.57.0. I would recommend double checking your pipeline to see if anything has been changed.