Is there a way to create a new User with existing Role using Rest API

I am using OpenMRS version 2.0.6 with openmrs-module-webservices.rest version 2.18.0. Is there a way to create a new User and assign an existing role to that user through the Rest API? Below is an example of what I tried, but instead of assigning the existing role to the user, a new role was created, which actually deleted the existing role and all of its associated role_privilege records:

{ “password”: “testPassword1”, “person”: “uuid”, “roles”: [{“role”:“testRole”}], “username”: “testUserName1” }

Note: I also tried using the uuid of an existing role, but a new role was created with a name that matched the uuid that was used.

I have also noticed that if I try to create a new Role using the REST API, I can only assign one privilege, even though the property is called “privileges” which makes me think I should be able to assign multiple privileges.

I think I figured out what I need to do. The steps below seem to work for me:

  1. Create the new user through the API without specifying the roles property
  2. Edit the user through the API, now include the roles property with it set to an existing role

You should be able to create the user along with roles in one request, I think the mistake you’re making is to specify the list of roles as names rather than uuids.

Just to be clear, the roles should be a list of uuids and not a map, below is an example:

{ “password”: “testPassword1”, “person”: “uuid”, “roles”: [“uuid-1”, ”uuid-2”], “username”: “testUserName1” }

I’ll give that a try. This is what I had initially tried using the uuid:

“roles”: [{“role”:“uuid”}]

Thanks for looking into this. That worked. The mistake I was making was that I was trying this “roles”: [{“role”:“uuid-1”,“role”:“uuid-2”}] instead of this “roles”: [“uuid-1”,“uuid-2”]

I tried creating the same with the below and its throwing an error

POST: http://localhost/openmrs/ws/rest/v1/user

    {
        "username": "test_user",
        "password": "Admin123",
        "person": {
            "names": [
                {
                    "givenName": "Test",
                    "middleName": "",
                    "familyName": "user"
                }
            ],
            "gender": "M"
        },
       "roles": [
                "8d94f280-c2cc-11de-8d13-0010c6dffd0f"
        ]
    }

ERROR:

 "code": "org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.UserResource1_8:358",
 "detail": "org.openmrs.module.webservices.rest.web.response.ConversionException: person\n\tat org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.UserResource1_8.setProperty(UserResource1_8.java:358)\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setConvertedProperties(BaseDelegatingResource.java:656)\n\tat org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.convert(DelegatingCrudResource.java:107)\n\tat org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.create(DelegatingCrudResource.java:76)\n\tat org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController.create(MainResourceController.java:92)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:177)\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:446)\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:434)\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:650)\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:731)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:73)\n\tat org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:65)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:71)\n\tat org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:105)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:71)\n\tat org.openmrs.module.webservices.rest.web.filter.ContentTypeFilter.doFilter(ContentTypeFilter.java:64)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:71)\n\tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:82)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:71)\n\tat org.openmrs.module.owa.filter.OwaFilter.doFilter(OwaFilter.java:57)\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:71)\n\tat org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:57)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:109)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:110)\n\tat org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:169)\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)\n\tat org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:1025)\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:445)\n\tat org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1137)\n\tat org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:637)\n\tat org.apache.tomcat.util.net.AprEndpoint$SocketWithOptionsProcessor.run(AprEndpoint.java:2512)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)\n\tat java.lang.Thread.run(Thread.java:748)\nCaused by: org.openmrs.module.webservices.rest.web.response.ConversionException: person on class org.openmrs.User\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setProperty(BaseDelegatingResource.java:789)\n\tat org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.UserResource1_8.setProperty(UserResource1_8.java:354)\n\t... 75 more\nCaused by: org.openmrs.module.webservices.rest.web.response.ConversionException: roles on class org.openmrs.Person\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setProperty(BaseDelegatingResource.java:789)\n\tat org.openmrs.module.webservices.rest.web.ConversionUtil.convertMap(ConversionUtil.java:332)\n\tat org.openmrs.module.webservices.rest.web.ConversionUtil.convert(ConversionUtil.java:273)\n\tat org.openmrs.module.webservices.rest.web.ConversionUtil.convert(ConversionUtil.java:155)\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setProperty(BaseDelegatingResource.java:784)\n\t... 76 more\nCaused by: java.lang.NullPointerException\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setProperty(BaseDelegatingResource.java:781)\n\t... 80 more\n"

Hi @rmkanda

Api definition are getting changed by time to time.The request body contains different set of attributes now

Please refer the swagger documentation to get an idea about the data to provide for the endpoint.

https://demo.openmrs.org/openmrs/module/webservices/rest/apiDocs.htm (Go to the user creating endpoint)

@ayesh I started with the API docs only. It dint help, so searched for any article about that in community already and found this post. This also dint help.

Hi @rmkanda

Sorry I didnt check this request body provided by u

    {
    "username": "test_user",
    "password": "Admin123",
    "person": {
        "names": [
            {
                "givenName": "Test",
                "middleName": "",
                "familyName": "user"
            }
        ],
        "gender": "M"
    },
   "roles": [
            "8d94f280-c2cc-11de-8d13-0010c6dffd0f"
    ]
}

This works fine for me.And creating a user as well.

Did u add the auth header to the request (authorization: Basic admin:Admin123)