Adding group members vs adding owners: data format is different

AI-generated summary

In the Discourse REST API, adding group members and owners requires different data formats: usernames={username} for members, and group[usernames]={username} for owners. bennett noticed this inconsistency is breaking pydiscourse, the primary Python library for Discourse API interactions. blake explains the difference stems from two separate controllers — the regular groups controller for members and the admin/groups controller for owners. While this is unlikely to change due to existing integrations, blake suggests opening an issue in the pydiscourse repo or applying a monkey patch to add_group_owner as a temporary workaround. The inconsistency, though inconvenient, appears to be intentional and entrenched in the API’s design.

When using the API to add members and owners to a group, it looks like the required data format is different. This may be intentional but I’d like to know why (it’s breaking pydiscourse, the best library I can find for interacting with the Discourse API in Python).

Adding a member to a group:

curl -X PUT "https://{mydomain}/admin/groups/{group_id}/members.json" \ 
    -d 'usernames={my-username}' \
    -H "Api-Key: {my-api-key}" \
    -H "Api-Username: {my-api-username}"

Adding an owner to a group:

curl -X PUT "https://{mydomain}/admin/groups/{group_id}/owners.json" \ 
    -d 'group[usernames]={my-username}' \
    -H "Api-Key: {my-api-key}" \
    -H "Api-Username: {my-api-username}"

I’m curious why the key for the username param in the owners endpoint needs to be written as group[usernames] instead of just usernames?

The only reason I can give as to why is that they are actually using two different controllers.

The add members endpoint is mapped to the groups controller:

PUT /admin/groups/:id/members(.:format) groups#add_members 

And the add owners endpoint is mapped to the admin groups controller.

PUT /admin/groups/:id/owners(.:format) admin/groups#add_owners

It’s unlikely we will update this endpoint because lots of integrations already use this endpoint as is. You best bet is to open up an issue in the pydiscourse repo. You could also monkey patch a fix for add_group_owner in your python app until it gets resolved inside of pydiscourse.