Help, Managnment API, set role for a user report 400 error

New to Auth0, try to use management API to set role for a user, GET works fine , but POST not , Anyone can see what I did wrong in the code below? Thanks

(user_id=‘auth0|65e738267a1c972e29248c8c’)

        import requests
        from django.conf import settings
        ...
        url = f"https://{settings.AUTH0_DOMAIN}/api/v2/users/{user_id}/roles"
        headers = {
            "Accept": "application/json",
            "Authorization": f"Bearer {self.mgm_access_token}",
        }
        payload = {}
        # url and access token is ok, 
        # GET returns 
        # '[{"id":"rol_y8KUM23pHuq9It1W","name":"customer","description":"external user"}]'
        response = requests.request("GET", url, headers=headers, data=payload)

        # POST reports 400 error, Bad Request, 
        # b'{"statusCode":400,"error":"Bad Request","message":"Payload validation error: 
        # \'Expected type array but found type string\' on property roles (List of roles IDs 
        # to associated with the user).","errorCode":"invalid_body"}'
        payload = {"roles": ["rol_y8KUM23pHuq9It1W"]}
        response = requests.request("POST", url, headers=headers, data=payload)

FYI, same error, when payload = {“roles”: [“customer”]}, similar error payload = {“roles”: [{“name”:“customer”}]} etc, just could not figure out the right payload format for the request body…or sth. else is missed?

Hey there @george.huang welcome to the community!
Does the request work if you change:

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

to:

response = requests.request("POST", url, headers=headers, json=payload)

1 Like

Yes , that works, Thanks for the quick response, nice weekend

1 Like

Awesome, thanks for confirming! :slight_smile:

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