Keycloak - PATCH user attributes through REST API
Asked Answered
S

1

6

I am using Keycloak docker image 21.0.2 (the latest one).

I make a PUT request to this URL: http://localhost:8080/admin/realms/custom-realm/users/14905db1-5229-4110-bfbb-df13845a4f52 and with this JSON body to edit a user's attributes:

{
    "attributes": {
        "organization_id": "1"
    }
}

The problem is that this wipes out all user attributes and I want it to keep the old ones as they are used for other functionalities and cannot be wiped out.

I just want to append or replace 'organization_id' value in the list of user attributes.

I think it should be a PATCH request but Keycloak doesn't have one for users. Is there any way to make this work?

I tried to update user attributes through the ADMIN REST API but I think a PATCH method is missing...

Scalp answered 13/4, 2023 at 9:15 Comment(8)
You can do it by "PUT {Keycloak URL}/admin/realms/{realm-name}/users/{user-id}" in hereSportive
Hi there, This does not work, as explained in my comment it will override ALL attributes of the specific user and what I want is override 1 attribute in the list, not all of them...Scalp
There is no PATCH command, you need to get attributes first and then to update the organization_id with other attributes to keep by PUT command.Sportive
Thanks for your answer. Yes I was thinking the same and wondering if there was any other way to do that but it looks like a PATCH command is missing in the API...Scalp
@Scalp with 23.0.6 - when using the Java Rest client at least - PUT seems to behave like a patch would. Maybe this helpsJameljamerson
Did you try replacing a specific attribute ? not the entire list ?Scalp
@Scalp I updated the enabled flag and the role, using the UserResource.update() method and all other attributes (firstname, email, other attributes) stayed the same.Jameljamerson
Is there any way to know the exact http request sent by your Java rest client ? (body, method, headers, endpoint?)Scalp
I
3

I had the exact same issue and i finally resolve it by fetching first the information of the user and made a custom merge when updating the user with the keycloak admin client. It's not a fancy solution, but Keycloak at this date does not provide a way to partially update the user.

Iinde answered 20/3, 2024 at 21:23 Comment(1)
I opened an open issue on their github for this, but it is not prioritary... github.com/keycloak/keycloak/issues/19691Scalp

© 2022 - 2025 — McMap. All rights reserved.