Error when trying to remove roles from user

When I’m trying to remove roles from a user I get “Invalid request payload JSON format” error.

public static async Task<string> RemoveRolesFromUser(string user_id, string[] roles, string token) {
    TaskCompletionSource<string> task = new TaskCompletionSource<string>();
    RestClient client = new RestClient($"https://x.eu.webtask.io/x/api/users/{user_id}/roles");
    RestRequest request = new RestRequest(Method.DELETE);

    request.AddHeader("Content-type", "application/json");
    request.AddHeader("Authorization", $"Bearer {token}");
    request.AddParameter("application/json", roles, ParameterType.RequestBody);
    request.RequestFormat = DataFormat.Json;

    client.ExecuteAsync(request, res => {
        if (res.ResponseStatus == ResponseStatus.Completed) {
            task.SetResult(res.Content);
        };
    });
    ...
}

Switching Method.DELETE to Method.PATCH for adding roles works just fine so I don’t understand why DELETE would throw a format error.

Also, in the API explorer it says “/users/{role_id}/roles” as endpoint but I think it’s a typo? It should be “/users/{user_id}/roles”, right? In the example it says “DELETE https://{extension_url}/users/{user_id}/roles” Authorization Extension API Explorer

Found the issue, AddParameter isn’t parsing the array accordingly. This worked:

request.AddParameter("application/json", JsonConvert.SerializeObject(roles), ParameterType.RequestBody);

Thanks a lot for sharing it with the rest of community!

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