REST issue with a fragment

Hello, I am new to OpenMRS. I am facing a REST API issue with Encounter Diagnosis. The data doesn’t gets posted. Error is 400 bad request. Can any one help me out with this? Also I am working with Angular if it is of any help.

Hi @hardikk08,

And what’s the URL that produces this?


Is the module REST Web Services running on your OpenMRS instance?

Yes it is!

Ok I see, your URL is valid, but not the REST request behind it. You are attempting to fetch an encounter without providing any other clues.

To give a sense of the sort of response that you could get, you could try this:


Where a pretty versatile query search string could be used for patient names or patient identifiers.

This would give you some results on the demo for instance:

@hardikk08 it’s good to provider details of the errors you’re getting when seeking help.

Uploading payload and preview. There is some problem with the patient uuid which is not being passed.

error_Trace.txt (7.2 KB)

Post your payload.

The patient parameter is not resolved:

patient: "${ patient.uuid }"

Thanks for reverting back. Yes i get it but I cannot figure how do i solve it.

Also i find this weird, check the screenshot. The obs for that particular fragment is posting patient id instead of patient uuid.

Here is the code -

Regarding the original issue, this is the type of JavaScript code that looks suspicious:

var patient = "${ patient.uuid }";

There are 3 such occurrences on that page. This will indeed just hardcode the string "${ patient.uuid }" in the JavaScript variables on the LHS.

Rather try to remove those quotes on the RHS:

... = ${patient.uuid};

Or even

... = ${patient};

and in that case get whatever you need out of the patient object, such as its UUID for example.

No success with any of the above. Actually in other fragments also i have used the same way to pass patient uuid. But they are working fine, its only with this fragment. Any other suggestions? If you could trying running on local server an₹ further help me debugging the issue, it would be of great help!

“No success” doesn’t tell us what you actually experienced. So…
When you try this kind of variable assignment (no surrounding quotes):

var patientUuid = ${patient.uuid};

Upon debugging, what do you get in patientUuid on the LHS?

It would have been helpful to point us to those working fragments, in particular to the exact places where you do operate the Groovy-to-JavaScript variable assignments.

I skimmed through your repo, and I found many similar variable assignments indeed. I suspect that they all do the exact same thing anyway, i.e. they hardcode the string "${ patient.uuid }" into the LHS JavaScript variable. I also suspect that those fragments might work simply because they don’t leverage the patient UUID down the line, or they keep relying implicitly on the Groovy patient variable that is passed to nested fragments.

Actually this is coded by someone else and I am trying to fix this issue. I don’t know much about groovy and particularly new to OpenMRS.

Ok so when I use this,

var patientUuid = ${patient.uuid};

a. Obs doesn’t gets posted upon inspecting b. I cannot log value of patientUuid c. But however it is visible in the element tab that patientUuid = (whatever it is)

Check the screenshot, in first factory that original author used var patient = “${ patient.uuid }” This one simply takes it as a string I believe In second, var patient = ${ patient.uuid }

Ok so let’s pedal back a little, are you saying that the first REST call works and the second doesn’t? Meaning that this works fine, but this doesn’t?

IF that’s what you’re experiencing, I can speculate a little and could imagine that this:

url += "?patient=" + patient;

may produce a slightly different result than this:

patient: patient,

In the first case the concatenation might force the string value resolution while in the second case not.

Nevertheless if the above is what you experience, you have enough similarity between the two factories to refactor the non-working one and make its behaviour converge towards the working one.

As per your screenshots, you may also try this in for the second factory:

var patientJs = ${patient};
var patient = patientJs.uuid;

Or if the problem persists that the surrounding quotes are missing (as per your screenshot), you can try this:

var patient = "'" + ${patient.uuid} + "'";

Or play around with


Ok, some further investigation into the database, I can see that the encounter diagnoses value is being posted into the database but it cannot be fetched. I have further realized that there is actually no code to retrieve the data. Uploading postman stats