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.

Unable to create role assignment via az rest: ValueError: not enough values to unpack (expected 2, got 1)

See original GitHub issue

Describe the bug

When trying to create a role assignment as shown here, I cannot seem to bypass the traceback error detailed below. However, this DOES work when executed via cURL.

Command Name az rest

Errors:

The command failed with an unexpected error. Here is the traceback:
not enough values to unpack (expected 2, got 1)
Traceback (most recent call last):
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/util.py", line 538, in shell_safe_json_parse
    return json.loads(json_or_dict_string, strict=strict)
  File "/opt/az/lib/python3.8/json/__init__.py", line 370, in loads
    return cls(**kw).decode(s)
  File "/opt/az/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/az/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/util.py", line 844, in send_raw_request
    temp = shell_safe_json_parse(s)
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/util.py", line 563, in shell_safe_json_parse
    raise InvalidArgumentValueError(msg, recommendation=recommendation) from json_ex
azure.cli.core.azclierror.InvalidArgumentValueError: Failed to parse JSON: Content-Type: application/json
Error detail: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/az/lib/python3.8/site-packages/knack/cli.py", line 231, in invoke
    cmd_result = self.invocation.execute(args)
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/commands/__init__.py", line 658, in execute
    raise ex
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/commands/__init__.py", line 721, in _run_jobs_serially
    results.append(self._run_job(expanded_arg, cmd_copy))
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/commands/__init__.py", line 692, in _run_job
    result = cmd_copy(params)
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/commands/__init__.py", line 328, in __call__
    return self.handler(*args, **kwargs)
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler
    return op(**command_args)
  File "/opt/az/lib/python3.8/site-packages/azure/cli/command_modules/util/custom.py", line 23, in rest_call
    r = send_raw_request(cmd.cli_ctx, method, url, headers, uri_parameters, body,
  File "/opt/az/lib/python3.8/site-packages/azure/cli/core/util.py", line 847, in send_raw_request
    key, value = s.split('=', 1)
ValueError: not enough values to unpack (expected 2, got 1)

To Reproduce:

Steps to reproduce the behavior. Note that argument values have been redacted, as they may contain sensitive information.

  • The az rest commands have been ran on both WSL Ubuntu and native PowerShell.
  • Doesn’t work:
    • az rest --method post --header "Content-Type: application/json" --url "https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments" --body '{"@odata.type": "#microsoft.graph.unifiedRoleAssignment", "roleDefinitionId": "<redacted>", "principalId": "<redacted>", "directoryScopeId": "/"}'
  • Works
    • curl -sL -H "Authorization: bearer $(az account get-access-token --scope https://graph.microsoft.com/.default --query accessToken -o tsv)" -H "Content-Type: application/json" "https://graph.microsoft.com/v1.0/roleManagement/directory/roleAssignments" -X POST --data '{"@odata.type": "#microsoft.graph.unifiedRoleAssignment", "roleDefinitionId": "<redacted>", "principalId": "<redacted>", "directoryScopeId": "/"}'

Expected Behavior

{
  "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#roleManagement/directory/roleAssignments/$entity",
  "id": "<redacted>",
  "principalId": "<redacted>",
  "directoryScopeId": "/",
  "roleDefinitionId": "<redacted>"
}

Environment Summary

Linux-4.4.0-19041-Microsoft-x86_64-with-glibc2.29, Ubuntu 20.04.4 LTS
Python 3.8.12
Installer: DEB

azure-cli 2.34.1

Extensions:
account 0.2.2
azure-devops 0.22.0
support 1.0.3

Dependencies:
msal 1.16.0
azure-mgmt-resource 20.0.0

Additional Context

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jiaslicommented, Mar 23, 2022

it complained about not having one, but as you pointed out it did work just now.

This is usually because the body is not a valid JSON, sometimes parsed by the shell, like double quotes ".

You are welcome. If you see any problem with az rest, please don’t hesitate to let us know. 😊

1reaction
jaredbrogancommented, Mar 23, 2022

@jaredbrogan, the failed line is:

https://github.com/Azure/azure-cli/blob/07819726e13d72587eb82b65c92ecba3bb009f32/src/azure-cli-core/azure/cli/core/util.py#L847

It fails because the header format is incorrect. Actually, you don’t need to specify header Content-Type at all:

> az rest -h

...
        If Content-Type header is not set and --body is a valid JSON string, Content-Type header
        will default to application/json.

Arguments
...
    --headers                         : Space-separated headers in KEY=VALUE format or JSON string.
                                        Use @{file} to load from a file.

Also, if you run the command in native PowerShell, pay attention to quoting issues: https://github.com/Azure/azure-cli/blob/dev/doc/quoting-issues-with-powershell.md

🤦‍♂️ Ugh… Thanks for pointing that out. I swear I tried it several times with and without the header. At one point without providing the header outright, it complained about not having one, but as you pointed out it did work just now.

Appreciate the sanity check on this one! 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

ValueError: not enough values to unpack (expected 2, got 1)
Unable to create role assignment via az rest: ValueError: not enough values to unpack (expected 2, got 1) ... This issue has been...
Read more >
webapp:ERROR: not enough values to unpack (expected 2 ...
Hi ... I am getting this error running the below command: az webapp config connection-string set -n myapp -g webapps -t SQLAzure --settings ......
Read more >
ValueError: not enough values to unpack (expected 2, got 1 ...
And you can't unpack (or assign) a single value into two variables. _, tmp = "random@gmail.com".split("@", maxsplit=1) It works.
Read more >
ValueError: not enough values to unpack (expected 2, got 1)
This error caused by the mismatch between the number of values returned and the number of variables in the assignment statement. This error...
Read more >
Remove Azure role assignments - Azure RBAC - Microsoft Learn
You should open Access control (IAM) at the scope where the role was assigned and try again. A quick way to open Access...
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