Could you please direct me to a link that provides the sample code to create a patient using rest API.
Hey @vundamat. I hope you have already seen the wiki. Are you looking for help in the request format for creating a patient, or in making a call in a specific language (say Java)?
Yes, I have seen the wiki. I was looking for some help with making a call in Java/PHP.
I created a person and got the uuid. But, I am not able to create a patient…its says “Invalid identifer”…do you know how I could solve this?
Hi, in:
you can find a create patient method using REST. I hope it helps you.
Thanks for the help guys! I figured out how do it!
This is my code:
package call1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.client.methods.HttpPost;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import sun.net.www.http.HttpClient;
import org.apache.http.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class Call1
{
public static void main(String[] args) throws Exception
{
//Call1.sendGet();
Call1.sendPost();
}
public static void sendGet() throws Exception
{
String encoding = DatatypeConverter.printBase64Binary("admin:Admin123".getBytes("UTF-8"));
String url = "http://localhost:8081/openmrs-standalone/ws/rest/v1/patientidentifiertype";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// Setting the request method
con.setRequestMethod("GET");
//adding the request headers
con.setRequestProperty("Authorization: Basic ",encoding);
//getting the response status
int responseCode = con.getResponseCode();
System.out.println("Sending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
public static void sendPost() throws Exception
{
//POST /openmrs/ws/rest/v1/patient
//HEADER: Content-Type: application/json
//uuid: 4ad0a62b-6238-40b6-9713-f06bb99993e5
//OpenMRS Id: 05a29f94-c0ed-11e2-94be-8c13b969e334
//Outpatient clinic: 58c57d25-8d39-41ab-8422-108a0c277d98
//RESPONSE: a newly created patient
String encoding = DatatypeConverter.printBase64Binary("admin:Admin123".getBytes("UTF-8"));
String url="http://loclahost:8081/openmrs-standalone/ws/rest/v1/patient";
URL object=new URL(url);
HttpURLConnection con = (HttpURLConnection) object.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
//adding the request headers
con.setRequestProperty("Authorization: Basic ",encoding);
con.setRequestProperty("Content-Type", "application/json");
con.setRequestMethod("POST");
//BODY: {"person": "11f40a1b-0337-4652-86fa-18d4461e38a0", "identifiers": [{"identifier":"1234", "identifierType":"8d79403a-c2cc-11de-8d13-0010c6dffd0f", "location":"8d6c993e-c2cc-11de-8d13-0010c6dffd0f", "preferred":true}]}
JSONObject pars=new JSONObject();
JSONObject jo = new JSONObject();
jo.put("identifier", "1003C3");
jo.put("identifierType", "05a29f94-c0ed-11e2-94be-8c13b969e334");
jo.put("location", "58c57d25-8d39-41ab-8422-108a0c277d98");
jo.put("preferred", "true");
JSONArray ja = new JSONArray();
ja.add(jo);
pars.put("person","7bcd18e4-0eb7-4959-be50-be5dc0df84f3");
pars.put("identifiers",ja);
System.out.println(pars);
//Sending the Post request
OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());
wr.write(pars.toString());
wr.flush();
int responseCode = con.getResponseCode();
System.out.println("Sending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null)
{
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
I hope this helps newbies like me
4 Likes