Is there a way to post data to datasets in dhis2?

Hello community, I would want to know if there is a way to post data from source to dhis2 datasets?

I would be glad if someone can share notes with me.

Thank you.

Hello @fosuhene Can you elaborate your question a bit more.

Did you mean, you have a source like xls or csv and you need to load that to DHIS2 datasets?

We have DHIS2 APIs to post/load data to datasets

A simple example from the documentation would like below.

Data example in csv format

Example of data being posted using API

1 Like

This example of posting a single data element to DHIS2 via the web API works well. However, when posting an entire dataset, i am getting challenges.
My question is how do you convert this curl command to a third party application? For example in Javascript? How do you reference the file name that has the data in particular? For example if its on the C drive on my computer?

See sample code below;

var url = “https://play.dhis2.org/2.37.0/api/dataValueSets”;

var xhr = new XMLHttpRequest();
xhr.open(“POST”, url);

xhr.setRequestHeader(“Content-Type”, “application/xml”);
xhr.setRequestHeader(“Authorization”, “Basic Username:Password”);

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};

var data = “C:\datavalueset.xml”; //This is where my problem is. How can i write this part?

xhr.send(data);

Hey @Micah,
Have you tried using Postman? After creating the request in Postman there’s a feature to get it in the code you want! It also helps with uploading files too! :smiley:

This example of how to post a single data element to DHIS2 via the web API works well.

curl "https://play.dhis2.org/demo/api/33/dataValues?de=s46m5MS0hxu
  &pe=201301&ou=DiszpKrYNg8&co=Prlt0C1RF0s&value=12"
  -X POST -u admin:district

However, when posting an entire dataset, i am getting challenges with this code:

curl -d @datavalueset.xml "https://play.dhis2.org/demo/api/33/dataValueSets"
  -H "Content-Type:application/xml" -u admin:district

My question is how do you convert this curl command to a third party application code? For example in Javascript or VBA or any other languge? How do you reference the file name that has the data in particular? For example if its on the C drive on my computer?

See sample code below;

var url = “DHIS 2 Demo - Sierra Leone”;

var xhr = new XMLHttpRequest();
xhr.open(“POST”, url);

xhr.setRequestHeader(“Content-Type”, “application/xml”);
xhr.setRequestHeader(“Authorization”, “Basic Username:Password”);

xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
console.log(xhr.status);
console.log(xhr.responseText);
}};

var data = “C:\datavalueset.xml”; //This is where my problem is. How can i write this part?

xhr.send(data);

Hello @Micah

I am doing something similar in nodejs, if that can help you here is the source code.

Regards

2 Likes

thank you @didate for sparing time to guide me

You are welcome, do not hesitate if you have questions on the source code

Thank you @Gassim ,

Yes I do use postman, although for only a week now. I opted for the desktop version. However, i have just checked the different menus but don’t see that option. Please do direct me where i can find it if possible.

1 Like

Hi @Micah,
You’ll learn it fast, no worries! After you create the request, check out the ‘Code Snippet’ in the right side of the Postman Desktop version screen:

Hahaha. It was right under my nose. Thank you @Gassim

1 Like

lol :rofl:
Good luck with your app!