# How do I create a patient using the REST API?

**URL:** <https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439>\
**Category:** Ask OpenMRS\
**Tags:** rest\
**Created:** [July 13, 2015, 6:27pm UTC](https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439 "2015-07-13T18:27:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vundamat](https://talk.openmrs.org/letter_avatar/vundamat/32/6_22734026db12803ebb3e059622ac3534.png) [@vundamat](https://talk.openmrs.org/u/vundamat)\
**Post date:** [July 13, 2015, 6:27pm UTC](https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439/1 "2015-07-13T18:27:44Z")

</div>

Could you please direct me to a link that provides the sample code to create a patient using rest API.

---

<div class="post-metadata">

**Author:** ![vinay](https://talk.openmrs.org/user_avatar/talk.openmrs.org/vinay/32/327_2.png) [@vinay](https://talk.openmrs.org/u/vinay)\
**Post date:** [July 14, 2015, 11:49am UTC](https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439/2 "2015-07-14T11:49:29Z")

</div>

Hey @vundamat. I hope you have already seen [the wiki](https://wiki.openmrs.org/display/docs/REST+Web+Services+API+For+Clients). Are you looking for help in the request format for creating a patient, or in making a call in a specific language (say Java)?

---

<div class="post-metadata">

**Author:** ![vundamat](https://talk.openmrs.org/letter_avatar/vundamat/32/6_22734026db12803ebb3e059622ac3534.png) [@vundamat](https://talk.openmrs.org/u/vundamat)\
**Post date:** [July 14, 2015, 4:58pm UTC](https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439/3 "2015-07-14T16:58:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![tmueller](https://talk.openmrs.org/user_avatar/talk.openmrs.org/tmueller/32/827_2.png) [@tmueller](https://talk.openmrs.org/u/tmueller)\
**Post date:** [July 14, 2015, 8:02pm UTC](https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439/4 "2015-07-14T20:02:24Z")

</div>

Hi, in:

> <https://github.com/openmrs/openmrs-contrib-uitestframework/blob/master/src/main/java/org/openmrs/uitestframework/test/TestBase.java>

you can find a create patient method using REST. I hope it helps you.

---

<div class="post-metadata">

**Author:** ![vundamat](https://talk.openmrs.org/letter_avatar/vundamat/32/6_22734026db12803ebb3e059622ac3534.png) [@vundamat](https://talk.openmrs.org/u/vundamat)\
**Post date:** [July 23, 2015, 4:33pm UTC](https://talk.openmrs.org/t/how-do-i-create-a-patient-using-the-rest-api/2439/5 "2015-07-23T16:33:41Z")

</div>

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 😄
