Usama Hameed
Usama Hameed

Reputation: 179

{'error': 'RESTEASY003650: No resource method found for PUT, return 405 with Allow header'} when updating user data in Keycloak

I am trying to update user info in keycloak by sending put request. Get request is working fine I am getting all the users but Whenever I tried to send put request to update the user data I get this error "{'error': 'RESTEASY003650: No resource method found for PUT, return 405 with Allow header'}" while searching for the solution I find somewhere that I should add 'HTTP_X_HTTP_METHOD_OVERRIDE' in headers I also tried this but still, I am facing same error, how can I fix it.

code:

def update_user(user_id, user_data):
    import requests
    headers = dict()
    headers['HTTP_X_HTTP_METHOD_OVERRIDE'] = 'PUT'
    headers['content_type'] = 'application/json'
    data = {
        "grant_type": "password",
        "username": "admin",
        "password": os.getenv("KEYCLOAK_ADMIN_KEY"),
        "client_id": "admin-cli"
    }
    token = _request("POST", f"{server_internal_url}realms/master/protocol/openid-connect/token", None, data=data).json()["access_token"]
    # token = admin_token()
    headers["Authorization"] = f"Bearer {token}"
    headers["Host"] = kc_host
    # here = _request("PUT", admin_url+f"/users"+"/".format(user_id=user_id), token, data=json.dumps(user_data)).json()

    response = requests.put(admin_url+f"/users"+"/".format(user_id=user_id), headers=headers, data=data, verify=VERIFY)

    print(response.json())

server_internal_url = "https://keycloak:8443/auth/"

admin_url = "https://keycloak:8443/auth/admin/realms/{realm_name}"

Upvotes: 0

Views: 12497

Answers (1)

Jan Garaj
Jan Garaj

Reputation: 28626

It looks like you request has wrong URL:

response = requests.put(admin_url+f"/users"+"/".format(user_id=user_id), headers=headers, data=data, verify=VERIFY)

I guess it should be:

response = requests.put(admin_url+"/users/"+user_id, headers=headers, data=data, verify=VERIFY)

Upvotes: 2

Related Questions