How do I fix a 400 Client Error: Bad Request for url, when trying to update user?

When I run this code from command line, it works as expected and the operation is successful:

curl -H "Authorization: Bearer <management_api_access_token>" -X PATCH  -H "Content-Type: application/json" -d '{"blocked":false}' https://<my_domain>.eu.auth0.com/api/v2/users/<user_id-including-auth-provider>

When I run the equivalent python script locally, I also get the expected successful response:

def do_this():
    user = "<user_id>"

    url = f"https://<my-domain>.eu.auth0.com/api/v2/users/{user}"

    payload = json.dumps({
      "blocked": True
    })
    headers = {
      "Authorization": "Bearer <management_api_access_token>",
      "Content-Type": "application/json",
      }

    response = requests.request("PATCH", url, headers=headers, data=payload)

    print(response.text)

However, when I run the python code in a lambda, I get this error:

400 Client Error: Bad Request for url: https://<my_domain>.eu.auth0.com/api/v2/users/<user_id>

I tried changing
requests.request("PATCH", url, headers=headers, data=payload)
to
requests.patch(url, data=payload, headers=headers)

but still same error in lambda

I have tried switching the user_id from
auth0|xxxxxx
to
auth0%7Cxxxxx
but still get the same error in lambda

(%7C is urlencoding of the pipe “|”)

Other calls I make to other management api endpoints in lambda seem to work fine, such as
/api/v2/users-by-email
and
/api/v2/users

What do I need to change in my code so that it will work universally?

I am working locally on MacOS, but my lambdas run in Linux.

Hi @Jez_D,

Welcome to the Auth0 Community!

Are you seeing any error message along with the Bad Request error? Typically you will see some more detail about why the request is failing. You may also want to check your Auth0 logs and see if there is any detail there.

Let me know,
Dan

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.