Create Complex Obs with REST API [SOLVED]

Version - OpenMRS 2.0.5 Rest Services version-2.23 I am trying to upload an image to a complex observation.I created a concept with complex type and imagehandler.When I try to use online , the image is getting uploaded and I can also download the image with the uri present in the response. When I try with REST api however it does not work.No error occurs , but no uri with value is getting generated.Could you let me know what is the correct payload that I should be passing . Is the feature available for 2.0.5 ?.The below is the sample json I was trying with.

{ “uuid”:“1639045f-35a7-4dc6-8608-ff6070fa53bc”, “encounterDatetime”: “2019-02-06T12:44:34.662+0530”, “patient”: “ffc30b9b-0de9-4c10-95e5-227043c8b45b”, “encounterType”: “8d5b27bc-c2cc-11de-8d13-0010c6dffd0f”, “visit”: “5b72525f-f553-4557-b7b1-06801ab2aad9”, “obs”: [ { “concept”: “8f0a07f6-2d51-4ea9-abb2-02779f2d78f5”, “value”: Base 64 encoded value of file as string } ], “encounterProviders”: [ { “encounterRole”: “73bbb069-9781-4afc-a9d1-54b6b2270e04”, “provider”: “163b48e5-26fb-40c1-8d94-a6c873dd2869” } ], “location”: “b56d5d16-bf89-4ac0-918d-e830fbfba290” }

How do I pass the filename also?.I tried putting filename field in the obs json , but it was throwing an error.So it’s going in as RAW.

@sajankos What do you have on your log ?

No i haven’t seen a functionality of passing the file-name directly to the URL of the obs resource. You can do the following.

the value parameter takes in a converted value of the file e.g

    InputStream in = getClass().getClassLoader().getResourceAsStream("customTestDataset.xml");
    		
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		IOUtils.copy(in, out);
String value = DatatypeConverter.printBase64Binary(out.toByteArray());

or you’ll just attach the actual file to the request like the way PostMan can do it ssss

But how will I post the json body along with a file in binary for a complex obs in postman?

you just post the usual json body , but leave out the value parameter,

and just upload the file via the “choose files” button , and the file will be attached to your request

Hi Moses, I am not sure whether I am doing something wrong but there doesn’t seem to be an option to send json body in binary in postman.I can attach a file but not sent a body.A screen shot with json body would help with binary.

@dkayiwa, what the easiest way to send both the json body and also attach a file for complex obs ??

This is a test showing how to post a complex obs with a base64 encoded value: 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#L199-L219

Then this other test shows how to post a complex obs file and name encoded as a multipart/form-data, with obs metadata included in a request parameter named ‘json’: 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#L221-L243

yes i went through that test , but am trying to figure out how to post that file in PostMan with the Json Body. it seems post man cant post both the json body and the file at ago

Did you see this in my response? "complex obs file and name encoded as a multipart/form-data, with obs metadata included in a request parameter named ‘json’ "

1 Like

Tried this also.called http://ipaddress/openmrs/ws/rest/v1/obs in postman.Getting the below error as attached in screenshot

You can see here that this is clearly supported: https://github.com/openmrs/openmrs-module-webservices.rest/blob/bff28f99d0866e8ea2589ff8ff6d9f29d38ec820/omod-common/src/main/java/org/openmrs/module/webservices/rest/web/v1_0/controller/MainResourceController.java#L96

Did you try making the same call from your application?

Just For Future reference i was trying to do the same thing , and i was succesfull after several trials , Ensure you run on the apropriate Version of Rest module , that contains the change set @dkayiwa posted above.
NB , Also make sure in your request header , you dont set Content-Type , PostMan will automatically set it for , other wise You’ll run into oanother error solved here

see Screenhot from my PostMAN

1 Like