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

**URL:** <https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827>\
**Category:** Get Help\
**Tags:** management-api, roles\
**Created:** [June 21, 2024, 8:41pm UTC](https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827 "2024-06-21T20:41:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![george.huang](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@george.huang](https://community.auth0.com/u/george.huang)\
**Post date:** [June 21, 2024, 8:41pm UTC](https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827/1 "2024-06-21T20:41:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 21, 2024, 9:32pm UTC](https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827/3 "2024-06-21T21:32:22Z")

</div>

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

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

```

to:

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

```

---

<div class="post-metadata">

**Author:** ![george.huang](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@george.huang](https://community.auth0.com/u/george.huang)\
**Post date:** [June 21, 2024, 9:46pm UTC](https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827/4 "2024-06-21T21:46:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [June 21, 2024, 10:17pm UTC](https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827/5 "2024-06-21T22:17:11Z")

</div>

Awesome, thanks for confirming! 🙂

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 5, 2024, 10:17pm UTC](https://community.auth0.com/t/help-managnment-api-set-role-for-a-user-report-400-error/136827/6 "2024-07-05T22:17:29Z")

</div>

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