REST API Implementation

hello, Can I know in what file do i type the code to access the REST API and is funcionality, I have read the REST API documentation but still couldnt decipher where to write the code to use the API’s. For example, the curl codes mentioned below,

// 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);

Or

GET /ws/rest/v1/resource?q=query = search

Can someone guide me where to write this? I am new to this, kindly help me.

Hi,

Those examples given by me were implementation of REST API using PHP. ( As I am using certain framework which uses MVC pattern, obviously this code comes under controller part ). We can implement this API in any language as you wish ( you have to follow the syntax rules of that language which you intend to use )

1 Like

Hi @selvars, Here you mentioned : // 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);

I’m not sure how you actually insert observation value? I can see value uuid instead original value.

It depends. If the value is a concept, then a concept uuid, but if it is something like weight, then the actual weight value, as you can see in these tests: https://github.com/openmrs/openmrs-module-webservices.rest/blob/master/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/ObsController1_9Test.java

1 Like