azure-cli icon indicating copy to clipboard operation
azure-cli copied to clipboard

"ERROR: Unsupported Media Type" when trying to use `az rest` via Azure DevOps pipelines

Open ehornby opened this issue 11 months ago • 3 comments

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 '' is not supported. Only 'application/json' is supported."}})

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..add_subscription_parameter at 0x7f1a07cd7ec0>] DEBUG: cli.knack.cli: Event: CommandInvoker.OnPostArgumentLoad [] DEBUG: cli.knack.cli: Event: CommandInvoker.OnPostCommandTableCreate [<function register_ids_argument..add_ids_arguments at 0x7f1a07d31c60>, <function register_cache_arguments..add_cache_arguments at 0x7f1a07d31da0>] DEBUG: cli.knack.cli: Event: CommandInvoker.OnCommandTableLoaded [] DEBUG: cli.knack.cli: Event: CommandInvoker.OnPreParseArgs [] DEBUG: cli.knack.cli: Event: CommandInvoker.OnPostParseArgs [<function OutputProducer.handle_output_argument at 0x7f1a08dba2a0>, <function CLIQuery.handle_query_parameter at 0x7f1a08defd80>, <function register_ids_argument..parse_ids_arguments at 0x7f1a07d31d00>] DEBUG: cli.azure.cli.core.util: unexpected character after line continuation character (, line 1) DEBUG: cli.azure.cli.core.util: Found subscription ID [[REDACTED]] in the URL https://management.azure.com/subscriptions/[[REDACTED]]/resourceGroups/[[REDACTED]]/providers/Microsoft.Web/sites/[[REDACTED]]/config/web?api-version=2022-03-01 DEBUG: cli.azure.cli.core.util: Retrieving token for resource https://management.core.windows.net/, subscription [[REDACTED]] DEBUG: cli.azure.cli.core.auth.persistence: build_persistence: location='/home/vsts/work/_temp/.azclitask/service_principal_entries.json', encrypt=False DEBUG: cli.azure.cli.core.auth.persistence: build_persistence: location='/home/vsts/work/_temp/.azclitask/msal_token_cache.json', encrypt=False DEBUG: cli.azure.cli.core.auth.binary_cache: load: /home/vsts/work/_temp/.azclitask/msal_http_cache.bin DEBUG: urllib3.util.retry: Converted retries value: 1 -> Retry(total=1, connect=None, read=None, redirect=None, status=None) DEBUG: msal.authority: openid_config = {'token_endpoint': 'https://login.microsoftonline.com/[[REDACTED]]/oauth2/v2.0/token', 'token_endpoint_auth_methods_supported': ['client_secret_post', 'private_key_jwt', 'client_secret_basic'], 'jwks_uri': 'https://login.microsoftonline.com/[[REDACTED]]/discovery/v2.0/keys', 'response_modes_supported': ['query', 'fragment', 'form_post'], 'subject_types_supported': ['pairwise'], 'id_token_signing_alg_values_supported': ['RS256'], 'response_types_supported': ['code', 'id_token', 'code id_token', 'id_token token'], 'scopes_supported': ['openid', 'profile', 'email', 'offline_access'], 'issuer': 'https://login.microsoftonline.com/[[REDACTED]]/v2.0', 'request_uri_parameter_supported': False, 'userinfo_endpoint': 'https://graph.microsoft.com/oidc/userinfo', 'authorization_endpoint': 'https://login.microsoftonline.com/[[REDACTED]]/oauth2/v2.0/authorize', 'device_authorization_endpoint': 'https://login.microsoftonline.com/[[REDACTED]]/oauth2/v2.0/devicecode', 'http_logout_supported': True, 'frontchannel_logout_supported': True, 'end_session_endpoint': 'https://login.microsoftonline.com/[[REDACTED]]/oauth2/v2.0/logout', 'claims_supported': ['sub', 'iss', 'cloud_instance_name', 'cloud_instance_host_name', 'cloud_graph_host_name', 'msgraph_host', 'aud', 'exp', 'iat', 'auth_time', 'acr', 'nonce', 'preferred_username', 'name', 'tid', 'ver', 'at_hash', 'c_hash', 'email'], 'kerberos_endpoint': 'https://login.microsoftonline.com/[[REDACTED]]/kerberos', 'tenant_region_scope': 'NA', 'cloud_instance_name': 'microsoftonline.com', 'cloud_graph_host_name': 'graph.windows.net', 'msgraph_host': 'graph.microsoft.com', 'rbac_url': 'https://pas.windows.net'} DEBUG: msal.application: Broker enabled? None DEBUG: cli.azure.cli.core.auth.msal_authentication: ServicePrincipalCredential.get_token: scopes=('https://management.core.windows.net//.default',), kwargs={} DEBUG: msal.application: Cache hit an AT DEBUG: msal.telemetry: Generate or reuse correlation_id: 6e78a133-00fe-454d-84c9-40a18b17b5e4 INFO: cli.azure.cli.core.util: Request URL: 'https://management.azure.com/subscriptions/[[REDACTED]]/resourceGroups/[[REDACTED]]/providers/Microsoft.Web/sites/[[REDACTED]]/config/web?api-version=2022-03-01' INFO: cli.azure.cli.core.util: Request method: 'PUT' INFO: cli.azure.cli.core.util: Request headers: INFO: cli.azure.cli.core.util: 'User-Agent': 'python/3.11.7 (Linux-6.5.0-1015-azure-x86_64-with-glibc2.35) AZURECLI/2.57.0 (DEB) VSTS_d96c9263-7ffa-4443-8a0b-6c70a06818f1_build_3072_0' INFO: cli.azure.cli.core.util: 'Accept-Encoding': 'gzip, deflate' INFO: cli.azure.cli.core.util: 'Accept': '/' INFO: cli.azure.cli.core.util: 'Connection': 'keep-alive' INFO: cli.azure.cli.core.util: 'x-ms-client-request-id': '51519242-bb0a-423c-8a52-7d40e6a46542' INFO: cli.azure.cli.core.util: 'CommandName': 'rest' INFO: cli.azure.cli.core.util: 'ParameterSetName': '--method --uri --body --debug' INFO: cli.azure.cli.core.util: 'Authorization': 'Bearer eyJ0eXAiOiJKV...' INFO: cli.azure.cli.core.util: 'Content-Length': '40' INFO: cli.azure.cli.core.util: Request body: INFO: cli.azure.cli.core.util: {"properties":{"http20ProxyFlag":2}} DEBUG: urllib3.connectionpool: Starting new HTTPS connection (1): management.azure.com:443 DEBUG: urllib3.connectionpool: https://management.azure.com:443 "PUT /subscriptions/[[REDACTED]]/resourceGroups/[[REDACTED]]/providers/Microsoft.Web/sites/[[REDACTED]]/config/web?api-version=2022-03-01 HTTP/1.1" 415 141 INFO: cli.azure.cli.core.util: Response status: 415 INFO: cli.azure.cli.core.util: Response headers: INFO: cli.azure.cli.core.util: 'Cache-Control': 'no-cache' INFO: cli.azure.cli.core.util: 'Pragma': 'no-cache' INFO: cli.azure.cli.core.util: 'Content-Length': '141' INFO: cli.azure.cli.core.util: 'Content-Type': 'application/json; charset=utf-8' INFO: cli.azure.cli.core.util: 'Expires': '-1' INFO: cli.azure.cli.core.util: 'x-ms-failure-cause': 'gateway' INFO: cli.azure.cli.core.util: 'x-ms-request-id': '73889a58-0da6-49b6-ae87-f2b9d516470c' INFO: cli.azure.cli.core.util: 'x-ms-correlation-request-id': '73889a58-0da6-49b6-ae87-f2b9d516470c' INFO: cli.azure.cli.core.util: 'x-ms-routing-request-id': 'CANADACENTRAL:20240301T202500Z:73889a58-0da6-49b6-ae87-f2b9d516470c' INFO: cli.azure.cli.core.util: 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains' INFO: cli.azure.cli.core.util: 'X-Content-Type-Options': 'nosniff' INFO: cli.azure.cli.core.util: 'X-Cache': 'CONFIG_NOCACHE' INFO: cli.azure.cli.core.util: 'X-MSEdge-Ref': 'Ref A: 997A92661E0249FDA4EE5510B283476F Ref B: YTO221090811049 Ref C: 2024-03-01T20:25:00Z' INFO: cli.azure.cli.core.util: 'Date': 'Fri, 01 Mar 2024 20:25:00 GMT' INFO: cli.azure.cli.core.util: Response content: INFO: cli.azure.cli.core.util: {"error":{"code":"UnsupportedMediaType","message":"The content media type '' is not supported. Only 'application/json' is supported."}} DEBUG: cli.azure.cli.core.azclierror: Traceback (most recent call last): File "/opt/az/lib/python3.11/site-packages/knack/cli.py", line 233, in invoke cmd_result = self.invocation.execute(args) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/commands/init.py", line 664, in execute raise ex File "/opt/az/lib/python3.11/site-packages/azure/cli/core/commands/init.py", line 729, in _run_jobs_serially results.append(self._run_job(expanded_arg, cmd_copy)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/commands/init.py", line 698, in _run_job result = cmd_copy(params) ^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/commands/init.py", line 334, in call return self.handler(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler return op(**command_args) ^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/command_modules/util/custom.py", line 24, in rest_call r = send_raw_request(cmd.cli_ctx, method, url, headers, uri_parameters, body, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/opt/az/lib/python3.11/site-packages/azure/cli/core/util.py", line 1004, in send_raw_request raise HTTPError(reason, r) azure.cli.core.azclierror.HTTPError: Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '' is not supported. Only 'application/json' is supported."}})

ERROR: cli.azure.cli.core.azclierror: Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '' is not supported. Only 'application/json' is supported."}}) ERROR: az_command_data_logger: Unsupported Media Type({"error":{"code":"UnsupportedMediaType","message":"The content media type '' is not supported. Only 'application/json' is supported."}}) DEBUG: cli.knack.cli: Event: Cli.PostExecute [<function AzCliLogging.deinit_cmd_metadata_logging at 0x7f1a07cc9bc0>] INFO: az_command_data_logger: exit code: 1 INFO: cli.main: Command ran in 0.642 seconds (init: 0.187, invoke: 0.455) 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 raise ex File "/opt/az/lib/python3.11/site-packages/azure/cli/main.py", line 55, in sys.exit(exit_code) SystemExit: 1

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 raise ex File "/opt/az/lib/python3.11/site-packages/azure/cli/main.py", line 55, in sys.exit(exit_code) SystemExit: 1

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

ehornby avatar Mar 01 '24 20:03 ehornby

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.

yonzhan avatar Mar 01 '24 20:03 yonzhan

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.

ehornby avatar Mar 01 '24 20:03 ehornby

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?

jiasli avatar Mar 04 '24 05:03 jiasli

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}}']

jiasli avatar Mar 04 '24 05:03 jiasli

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.

ehornby avatar Mar 05 '24 18:03 ehornby

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.

jiasli avatar Apr 08 '24 05:04 jiasli