How to create a person using rest API

Hello I am newbie to use openmrs, I want to create a person by using rest api.

I am using this endpoint to create a person bt post request

/openmrs/ws/rest/v1/person/

this is my request body

{
    "names": [
        {
        "givenName": "Mohit",
        "familyName": "Kumar"
        }
    ],
    "gender": "M",
    "birthdate": "1997-09-02",
    "addresses": [
        {
        "address1": "30, Vivekananda Layout, Munnekolal,Marathahalli",
        "cityVillage": "Bengaluru",
        "country": "India",
        "postalCode": "560037"
        }
    ]
}

I am getting this error Error message

Hey @mhmasuk,

I just looked at what our new O3 was doing (for a patient though), and it’s using /openmrs/ws/rest/v1/patient/.

An example payload being:

{
  "person": {
    "names": [
      {
        "preferred": true,
        "givenName": "John",
        "middleName": "",
        "familyName": "Doe"
      }
    ],
    "gender": "M",
    "birthdate": "1969-12-31T23:00:00.000Z",
    "birthdateEstimated": false,
    "attributes": [],
    "addresses": [
      {
        "address1": "Main Street",
        "country": "Uganda",
        "stateProvince": "",
        "cityVillage": "",
        "postalCode": ""
      }
    ],
    "dead": false
  },
  "identifiers": [
    {
      "identifier": "103EDG",
      "identifierType": "05a29f94-c0ed-11e2-94be-8c13b969e334",
      "location": "58c57d25-8d39-41ab-8422-108a0c277d98",
      "preferred": true
    }
  ]
}

Hopefully that will help you get going.

Thanks @mksd, for your reply. Still having the same issue I am using postman to send the request. I also include BasicAuth in header but no luck

Your payload works perfectly fine for me when I try it with Postman against demo.openmrs.org. In the “Body” tab, do you have it set to “Text” or “JSON” (it should be JSON).

1 Like

Thanks @ibacher, Yes you are right. This is working on demo.openmrs.org, but I was using Bahmni openMRS. Is there is some version related issue

Works perfectly fine for me against https://demo.mybahmni.org, but I can replicate the error you received if I have the request type sent to GET instead of POST…

I am also sending the request with POST method, This is working fine in https://demo.openmrs.org/, but getting error from Bahmni openMRS

Can you post the corresponding HTTP code snippet from Postman?

@ibacher here is the screenshoot

Well, that all looks correct to me, except for the error.

this payload works for me as well @mhmasuk, try exporting the request from Postman so we can see the actual request :+1:

Thanks @saurabh, You can download the file from here https://filebin.net/q1jobqu8sw95ruko

POST /openmrs/ws/rest/v1/person/ HTTP/1.1
Host: demo.openmrs.org
Content-Type: application/json
Authorization: Basic YWRtaW46QWRtaW4xMjM=
Cookie: JSESSIONID=B0E599307268AFE91127026D5EA2D63B
Content-Length: 407

{
    "names": [
        {
            "givenName": "Mohit",
            "familyName": "Kumar"
        }
    ],
    "gender": "M",
    "birthdate": "1997-09-02",
    "addresses": [
        {
            "address1": "30, Vivekananda Layout, Munnekolal,Marathahalli",
            "cityVillage": "Bengaluru",
            "country": "India",
            "postalCode": "560037"
        }
    ]
}

can you import this and see if it works? I edited your request and it worked.

1 Like

What’s running on port 80 of the machine you are trying to connect to? The error you’re seeing is 100% reproducible if you use http://demo.openmrs.org rather than https://demo.openmrs.org. This is because the response to the initial POST request (on demo.openmrs.org) is:

HTTP/1.1 301 Moved Permanently
Connection: keep-alive
Content-Length: 194
Content-Type: text/html
Date: Mon, 07 Jun 2021 19:13:03 GMT
Location: https://demo.openmrs.org/ws/rest/v1/person/
Server: nginx/1.10.3 (Ubuntu)
Strict-Transport-Security: max-age=15768000

<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.10.3 (Ubuntu)</center>
</body>
</html>

Which then results in Postman doing a GET request to https://demo.openmrs.org/ws/rest/v1/person/ with the request body, which is what gives you the error. For https://demo.openmrs.org, this can be resolved by changing the request from http → https. Presumably doing the same thing with your local server should work…

PS, I discovered this by looking at the Postman console.

2 Likes

Thank you very much @ibacher, Problem is solved now.