Create new user using java rest client

I am trying to create a user using the spring REST client. When I pass the user with “rep” key I get: 400 Bad Request: [Unrecognized field “rep” (class org.keycloak.representations.idm.UserRepresentation), not marked as ignorable]

public String createUser2() throws UnsupportedEncodingException, JSONException {

	String adminToken = getAccessToken();
	
    System.out.println("token: " + adminToken);

	HttpHeaders headers = new HttpHeaders();
	headers.set("Content-Type", "application/json");
	headers.set("Authorization", "Bearer " + adminToken);
	
	UserRepresentation userRepresentation = new UserRepresentation();
	userRepresentation.setFirstName("some");
	userRepresentation.setLastName("user");
	userRepresentation.setUsername("Some...@trilliant.com");
	userRepresentation.setEmail("Some...@trilliant.com");
	
	Gson gson = new Gson();
	String jsonUser = gson.toJson(userRepresentation);
	
	MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
	map.add("rep", jsonUser);
	
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
    
    String realm = "12xDemo";
    String uri = keycloakUsersUri.replace("$REALM", realm);
    System.out.println("URI: " + uri);
    
    String response = (new RestTemplate()).postForObject(uri, request, String.class);
    JSONObject obj = new JSONObject(response);

    return obj.toString();
}

The documentation is misleading in this case. It doesn’t want a map with rep as one of the params, it wants the user json representation as the POST body.

For a good example, use the admin UI to create a new user, and watch the network inspector for the POST /{realm}/users.