REST API Implementation

Hi,

I am Selva R. I am using openMRS standalone ( version 2.3.1 ) for testing purpose. I am trying to add patient data from another server ( using REST API - cURL ) to openMRS and i am able to add successfully. But while trying to add visits,encounters and observations i am receiving an error ( Here I pasted the error log for encounter )

{“error”:{“message”:“Unable to convert object into response content”,“code”:“org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource:758”,“detail”:“org.openmrs.module.webservices.rest.web.response.ConversionException: patient on class org.openmrs.Encounter\r\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setProperty(BaseDelegatingResource.java:758)\r\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setConvertedProperties(BaseDelegatingResource.java:612)\r\n\tat org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.create(DelegatingCrudResource.java:93)\r\n\tat org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController.create(MainResourceController.java:86)\r\n\tat sun.reflect.GeneratedMethodAccessor660.invoke(Unknown Source)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:606)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:643)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:723)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:61)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:72)\r\n\tat org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:64)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\r\n\tat org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:108)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\r\n\tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:73)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\r\n\tat org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:54)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:109)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)\r\n\tat org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)\r\n\tat org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:610)\r\n\tat org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1777)\r\n\tat java.lang.Thread.run(Thread.java:745)\r\nCaused by: org.openmrs.api.APIAuthenticationException: Privileges required: Get Patients\r\n\tat org.openmrs.aop.AuthorizationAdvice.throwUnauthorized(AuthorizationAdvice.java:121)\r\n\tat org.openmrs.aop.AuthorizationAdvice.before(AuthorizationAdvice.java:98)\r\n\tat org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:50)\r\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)\r\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)\r\n\tat com.sun.proxy.$Proxy115.getPatientByUuid(Unknown Source)\r\n\tat org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8.getByUniqueId(PatientResource1_8.java:217)\r\n\tat org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_8.PatientResource1_8.getByUniqueId(PatientResource1_8.java:50)\r\n\tat org.openmrs.module.webservices.rest.web.ConversionUtil.convert(ConversionUtil.java:229)\r\n\tat org.openmrs.module.webservices.rest.web.ConversionUtil.convert(ConversionUtil.java:149)\r\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setProperty(BaseDelegatingResource.java:753)\r\n\t… 64 more\r\n”}}

I am not able to track what the wrong i am doing here. Based on the above log, I could see like privileges are not sufficient for this query. But this user is a system developer ( stated as having all privileges ). Please point me in the right dirtection

[ Note:- I am able to add visits, encounters etc., using the test page provided in the openMRS interface itself. It fails only through the cURL call

   // encounter
   $enc_data['patient']                    = "uuid of the patient";  
   $enc_data['encounterDatetime']  = "encounter date";
   $enc_data['encounterType']      = "encounter type";
   
   $enc_data = json_encode($enc_data);
   
   $ch = curl_init('http://localhost:9999/openmrs-standalone/ws/rest/v1/encounter');
   curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_ANY);
   curl_setopt($ch, CURLOPT_USERPWD,"Admin:Admin123");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
   curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
   curl_setopt($ch, CURLOPT_POST,1);
   curl_setopt($ch, CURLOPT_POSTFIELDS,$enc_data);
   $encoutput   = curl_exec($ch);
   $encstatus   = curl_getinfo($ch,CURLINFO_HTTP_CODE);
   curl_close($ch); 

 sample rest call for kind perusal  ]

Thanks in advance.

Selva R

Have you read the REST documentation about authentication?

I believe you need “admin” in lowercase as the username, and also you may need to actually encode this.

Hi,

Thanks for the response. Actually I changed the username ( admin as Admin ). That’s why i used like this. Also I am using the same format ( without encoding ) for the person and patient creation respectively. Its working fine in both the cases. I will try the way as you suggested and i will update.

Selva R

The important part of the stack trace in your first message is:

Caused by: org.openmrs.api.APIAuthenticationException: Privileges required: Get Patients

Are you sure your Admin user has the required privileges?

Hi Mr.Pascal,

Thanks for the response.Yeah, I am sure. Admin user’s role is a system developer ( who has all privileges as mentioned ). Let me try with some new user and I will update you the proceedings.

Hi,

I created a new role with all privileges granted ( for testing purpose ) and created a new user and assigned this role. I am trying the same activity with this user,still I am receiving the same error. Please help me to sort out this issue.

[ Note:- person creation and patient creation working fine whereas for adding visits,encounters and observations i am receiving this privilege required error ]

Hi,

@darius Actually I was wrong ( I mentioned as without encoding ). The code

         curl_setopt($ch, CURLOPT_USERPWD,"Admin:Admin123");

is actually encoding the username and password ( base64 encoding ) ( the way openMRS defined )

@darius, @pascal Now I am able to add visits and encounter data by changing ( http auth option from any to basic )

curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);

However I am facing issue in adding observations. While trying to add observation,

     {
       "person":"64c05611-bdb0-49d6-acff-b3f5dbaa63c7",
       "obsDatetime":"2016-04-18",
       "concept":"dd49078a-d617-11e5-bf32-74d02b7ecd50"
     }

following error is received

  {
     "error":{
       "message":"Invalid Submission",
       "code":"webservices.rest.error.invalid.submission",
       "globalErrors":[{"code":"error.noValue","message":"error.noValue"}],
       "fieldErrors":{
         "valueCoded":[{"code":"error.null","message":"Cannot be empty or null"}]
                          }
               }
  }



   [ Note:-  I am not able to add observation data in the test page provided in the openMRS interface itself. There also receiving the same error. ]

Thanks in advance.

Can you provide the complete HTTP request and response?

Based on these API docs it looks like you need to provide a patient and not a person when creating an encounter. Have you tried that?

Yeah, Sure.

// observation $obs_data[‘person’] = “person uuid”; $obs_data[‘obsDatetime’] = “observation date”; $obs_data[‘concept’] = “concept id”;

So for ex., obs_data would be,

{ “person” :“64c05611-bdb0-49d6-acff-b3f5dbaa63c7”, “obsDatetime” :“2016-04-20”, “concept” :“dd49078a-d617-11e5-bf32-74d02b7ecd50” }

$obs_data = json_encode($obs_data);

$ch = curl_init('http://localhost:9999/openmrs-standalone/ws/rest/v1/obs');
curl_setopt($ch, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD,"Robo:Robo12345");
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$obs_data);
$obsoutput   = curl_exec($ch);
$obsstatus   = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);

I am trying to add observation.

As per this link i am providing data ( person ). I am not even able to add observation data in the REST API test page provided in the openMRS interface itself. There also receiving this error.

Am I missing anything ?

Can I see the HTTP request and response headers and bodies? What version of OpenMRS and Rest Web Services are you using?

Are you seeing the same error if you replace person with patient?

I highly recommend upgrading to the latest version of the Rest Web Services module (v2.14).

Based on this…

"globalErrors":[{"code":"error.noValue","message":"error.noValue"}],"fieldErrors":{"valueCoded":[{"code":"error.null","message":"Cannot be empty or null"}]}}}

… The issue is that you did not provide a value for the observation, thus it is invalid. In this case it should be a coded value.

1 Like

I am using openMRS standalone 2.3.1 and Rest webservices ( v2.12 )

If I replace patient with person, I am receiving some other error.

{"error":
    {"message":
       "Unable to convert object into response content","code":"org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource:629","detail":"org.openmrs.module.webservices.rest.web.response.ConversionException: Some properties are not allowed to be set: patient\r\n\tat org.openmrs.module.webservices.rest.web.resource.impl.BaseDelegatingResource.setConvertedProperties(BaseDelegatingResource.java:629)\r\n\tat org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.create(DelegatingCrudResource.java:93)\r\n\tat org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController.create(MainResourceController.java:86)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\r\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\r\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\r\n\tat java.lang.reflect.Method.invoke(Method.java:606)\r\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440)\r\n\tat org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)\r\n\tat org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)\r\n\tat org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:953)\r\n\tat org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:855)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:643)\r\n\tat org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:829)\r\n\tat javax.servlet.http.HttpServlet.service(HttpServlet.java:723)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ForcePasswordChangeFilter.doFilter(ForcePasswordChangeFilter.java:61)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:72)\r\n\tat org.openmrs.web.filter.GZIPFilter.doFilterInternal(GZIPFilter.java:64)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\r\n\tat org.openmrs.module.webservices.rest.web.filter.AuthorizationFilter.doFilter(AuthorizationFilter.java:108)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\r\n\tat org.springframework.web.filter.ShallowEtagHeaderFilter.doFilterInternal(ShallowEtagHeaderFilter.java:73)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.openmrs.module.web.filter.ModuleFilterChain.doFilter(ModuleFilterChain.java:70)\r\n\tat org.openmrs.module.web.filter.ModuleFilter.doFilter(ModuleFilter.java:54)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.OpenmrsFilter.doFilterInternal(OpenmrsFilter.java:109)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:230)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.openmrs.web.filter.StartupFilter.doFilter(StartupFilter.java:105)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)\r\n\tat org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)\r\n\tat org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)\r\n\tat org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)\r\n\tat org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)\r\n\tat org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)\r\n\tat org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)\r\n\tat org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)\r\n\tat org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)\r\n\tat org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879)\r\n\tat org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:610)\r\n\tat org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1777)\r\n\tat java.lang.Thread.run(Thread.java:745)\r\n"
}
}

HTTP Response status code is 500 (ie.,) Internal Server Error

Meanwhile I will update to the latest version and I will update you the proceedings

Hi,

Could you please explain a little more ? I am not able to understand about value for the observation. Whether I need to pass along with this data ? Meanwhile I will also refer about the coded value.

@pascal, @darius

Thank you for the immense support. Finally I figured it out. Now I am able to add observation data.

   { 
     "person":"f111c780-4f28-4984-886e-c94a8598eb4e",
     "obsDatetime":"2016-04-21",
     "concept":"dd49078a-d617-11e5-bf32-74d02b7ecd50",
     "value":"dd4f8192-d617-11e5-bf32-74d02b7ecd50"
  }

@darius As you said, i missed the value. First I got confused since you mentioned as value of the observation. Then I understood that you mean the value field ( As it is not a required field, I skipped it. But later I realized that since I am using the concept of coded datatype, I need to pass value also )

@pascal I updated to the latest version (v2.14) and it is working fine. Also I degraded to the early version ( REST API version (v2.12) which i used ) and checked. Everything is working fine

Thanks once again. And I have the responsibility of thanking another two guys who also supported @r0bby @michael . Thank you guys.

I feel like it will be better if documentation is provided with some examples for each part. So that newbies like me will not struggle so much in starting the implementation phase itself. I hope my request would be considered.

@selvars you do not have to ask for permission to improve the documentation. Just add what you think is helpful and the page watchers will guide accordingly. :slight_smile:

2 Likes

Ok Sure. Thank You

@selvars so are you actually going to do it? :smile:

Hi,

I may not be able to do that much, but I wish atleast I could provide my implementation details ( code examples ) which may be useful for the newbies ( as a simple start guide ) :smiley:

Whatever you can actually add to the wiki pages, it will be very useful and make life easier for lots more people. Am looking forward to seeing your additions to the documentation. :slight_smile:

I emphasize this because i have seen many talk about improving the documentation but never get to add even the smallest that they can. :wink: So be one of those to break this cycle. :slight_smile:

@dkayiwa Yeah sure. First I would like to add here my code ( examples ) which may finalize this topic with the minimal solution.

My implementation of REST API in php, ( I started with minimum details to kick start, as really targeting for HL7 message implementation. So now I may not be able to provide examples for all part )

$person_data        = array();
$name                 = array();
$names               = array();
$address             = array();
$addresses         = array();
$visit_data          = array();
$obs_data          = array();
$enc_data          = array();
$patient_data     = array();
$identifiers         = array();
$person_identify = array();

// PERSON

$address['address1']      = "Address 1";
$address['address2']      = "Address 2"; 
$address['cityVillage']   = "Name of the city";
$address['stateProvince'] = "Name of the state";
$address['country']       = "Name of the country";
$address['postalCode']    = "Postal code";
       
array_push($addresses,$address);
       
$name['givenName']  = "givenName";
$name['middleName'] = "middleName";
$name['familyName'] = "familyName";

array_push($names,$name);

$person_data['gender']    = "gender";
$person_data['birthdate'] = "date of birth";
$person_data['names']     = $names;
$person_data['addresses'] = $addresses;
       
$person_data = json_encode($person_data);

$person_curL = curl_init('http://localhost/openmrs-standalone/ws/rest/v1/person');
curl_setopt($person_curL, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($person_curL, CURLOPT_USERPWD,"admin:Admin123");
curl_setopt($person_curL, CURLOPT_RETURNTRANSFER,true);
curl_setopt($person_curL, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
curl_setopt($person_curL, CURLOPT_POST,1);
curl_setopt($person_curL, CURLOPT_POSTFIELDS,$person_data);
$output   = curl_exec($person_curL);
$status   = curl_getinfo($person_curL,CURLINFO_HTTP_CODE);
curl_close($person_curL); 
       
$output = json_decode($output);

// PATIENT

$identifiers['identifier']     = "identifier"; // user defined
$identifiers['identifierType'] = "uuid of the identifier type" // Either Old Identification number or OpenMRS Identification Number
$identifiers['location']       = "uuid of the location";
$identifiers['preferred']      = true;
       
array_push($person_identify,$identifiers);
       
$patient_data['person']      = $output->uuid;    // above created person's uuid ( we can also use already created person's uuid )
$patient_data['identifiers'] = $person_identify;

$patient_data = json_encode($patient_data);

$patient_curL = curl_init('http://localhost/openmrs-standalone/ws/rest/v1/patient');
curl_setopt($patient_curL, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($patient_curL, CURLOPT_USERPWD,"admin:Admin123");
curl_setopt($patient_curL, CURLOPT_RETURNTRANSFER,true);
curl_setopt($patient_curL, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
curl_setopt($patient_curL, CURLOPT_POST,1);
curl_setopt($patient_curL, CURLOPT_POSTFIELDS,$patient_data);
$patientoutput  = curl_exec($patient_curL);
$patientstatus  = curl_getinfo($patient_curL,CURLINFO_HTTP_CODE);
curl_close($patient_curL);

// ENCOUNTER

$enc_data['patient']            = $output->uuid;   // above created person's uuid  
$enc_data['encounterDatetime']  = "date";
$enc_data['encounterType']      = "encounter type";

$enc_data = json_encode($enc_data);

$enc_curL = curl_init('http://localhost/openmrs-standalone/ws/rest/v1/encounter');
curl_setopt($enc_curL, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($enc_curL, CURLOPT_USERPWD,"admin:Admin123");
curl_setopt($enc_curL, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
curl_setopt($enc_curL, CURLOPT_POST,1);
curl_setopt($enc_curL, CURLOPT_POSTFIELDS,$enc_data);
curl_setopt($enc_curL, CURLOPT_RETURNTRANSFER,true);
$encoutput   = curl_exec($enc_curL);
$encstatus   = curl_getinfo($enc_curL,CURLINFO_HTTP_CODE);
curl_close($enc_curL);

// VISIT

$visit_data['patient']       = $output->uuid;   // above created person's uuid
$visit_data['visitType']     = "visit type";
$visit_data['startDatetime'] = "Date";
       
$visit_data = json_encode($visit_data);
       
$visit_curL = curl_init('http://localhost/openmrs-standalone/ws/rest/v1/visit');
curl_setopt($visit_curL, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($visit_curL, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
curl_setopt($visit_curL, CURLOPT_USERPWD,"admin:Admin123");
curl_setopt($visit_curL, CURLOPT_POST,1);
curl_setopt($visit_curL, CURLOPT_POSTFIELDS,$visit_data);
curl_setopt($visit_curL, CURLOPT_RETURNTRANSFER,true);
$visitoutput = curl_exec($visit_curL);
$visitstatus = curl_getinfo($visit_curL,CURLINFO_HTTP_CODE);
curl_close($visit_curL);

// OBSERVATION

$obs_data['person']         = $output->uuid;             // above created person's uuid
$obs_data['obsDatetime']    = "date of the observation";
$obs_data['concept']        = "uuid of the concept";
$obs_data['value']          = "uuid of the value";

$obs_data = json_encode($obs_data);
       
$obscurL = curl_init('http://localhost/openmrs-standalone/ws/rest/v1/obs');
curl_setopt($obscurL, CURLOPT_HTTPAUTH,CURLAUTH_BASIC);
curl_setopt($obscurL, CURLOPT_USERPWD,"admin:Admin123");
curl_setopt($obscurL, CURLOPT_RETURNTRANSFER,true);
curl_setopt($obscurL, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); 
curl_setopt($obscurL, CURLOPT_POST,1);
curl_setopt($obscurL, CURLOPT_POSTFIELDS,$obs_data);
$obsoutput   = curl_exec($obscurL);
$obsstatus   = curl_getinfo($obscurL,CURLINFO_HTTP_CODE);
curl_close($obscurL);