DHIS2 , POST or pushing to another DHIS2 system

import requests
import json
from urllib.parse import urljoin

Set the credentials and URL for the target DHIS2 system

target_dhis_url = ‘https://3pm.nascop.org/
target_dhis_username = ‘username’
target_dhis_password = ‘password’

Authenticate to the target DHIS2 system

def authenticate_to_dhis(username, password):
auth = (username, password)
return auth

target_auth = authenticate_to_dhis(target_dhis_username, target_dhis_password)

Prepare and post data (example)

data_to_post = {
“dataValues”: [
“dataElement”: “xTpuBPLezo7”,
“period”: “202209”,
“orgUnit”: “zTjUwJx8Mw0”,
“categoryOptionCombo”: “QZXiPecjLhn”,
“attributeOptionCombo”: “LzQRfCHSx3P”,
“value”: 5

Construct the URL using urljoin

base_url = target_dhis_url
api_endpoint = ‘api/dataValueSets’
url = urljoin(base_url, api_endpoint)

headers = {‘Content-Type’: ‘application/json’}

Log the request URL for debugging

print(“Request URL:”, url)
response = requests.post(url, auth=target_auth, headers=headers, data=json.dumps(data_to_post))

if response.status_code == 201:
    print("Data posted successfully.")
    print(f"Failed to post data. Status code: {response.status_code}")

except requests.exceptions.RequestException as e:
print("An error occurred: ", e)

I am trying to post data in another DHIS2 system but am getting this error Request URL: https://3pm.nascop.org/api/dataValueSets
Failed to post data. Status code: 404

404 Not Found

404 Not Found

nginx/1.14.0 (Ubuntu)

kindly assist. Thanks

you probably need to change the base_url to https://3pm.nascop.org/dhis and not https://3pm.nascop.org

Thanks Stephen, although am still getting this error message :

Request URL: CRIMS - CIHEB Reporting Information Management System
Failed to post data. Status code: 200

CRIMS - CIHEB Reporting Information Management System .displayNoneClass { display: none; } .borderNoneClass { border: none; } .paddingTenClass { padding-bottom: 10px; } .whiteRedClass { color: white; background-color: red; } .marginLeftClass { margin-left: 30px; }

CRIMS - CIHEB Reporting Information Management System

CRIMS - CIHEB Reporting Information Management System
    <form id="loginForm" action="../../dhis-web-commons-security/login.action" method="post">
            <div id="signInLabel">Sign in</div>
            <div><input type="text" id="j_username" name="j_username" placeholder="Username" required></div>
            <div><input type="password" id="j_password" name="j_password" autocomplete="off" placeholder="Password"
                <input type="checkbox" name="2fa" value="2fa" id="2fa"/><label id="2FaLabel">Login using two factor authentication</label>
                <input type="code" id="2fa_code" name="2fa_code"
                       placeholder="Two factor authentication code" hidden readonly>
        <div id="submitDiv">
            <input id="submit" class="button" type="submit" value="Sign in">
                        <div class="paddingTenClass">
                <a id="forgotPasswordLink" href="recovery.action">Forgot password?</a>

                            <!--[if lte IE 8]>
        <div id="notificationArea" class="whiteRedClass">Please upgrade your browser. Internet Explorer version 8 and earlier is not supported.</div>
Powered by DHIS 2 
CIHEB-K (CIHEB Reporting Information Management System)

Kindly ignore the first error posted. Below is the error displayed
Request URL: 3PM
Failed to post data. Status code: 200
{“responseType”:“ImportSummary”,“status”:“SUCCESS”,“importOptions”:{“idSchemes”:{},“dryRun”:false,“async”:false,“importStrategy”:“CREATE_AND_UPDATE”,“mergeMode”:“REPLACE”,“reportMode”:“FULL”,“skipExistingCheck”:false,“sharing”:false,“skipNotifications”:false,“skipAudit”:false,“datasetAllowsPeriods”:false,“strictPeriods”:false,“strictDataElements”:false,“strictCategoryOptionCombos”:false,“strictAttributeOptionCombos”:false,“strictOrganisationUnits”:false,“requireCategoryOptionCombo”:false,“requireAttributeOptionCombo”:false,“skipPatternValidation”:false,“ignoreEmptyCollection”:false,“force”:false,“firstRowIsHeader”:true,“skipLastUpdated”:false,“mergeDataValues”:false,“skipCache”:false},“description”:“Import process completed successfully”,“importCount”:{“imported”:0,“updated”:1,“ignored”:0,“deleted”:0},“conflicts”:,“dataSetComplete”:“false”}
PS C:\Users\WImamai\OneDrive - Center For International Health, Education and Biosecurity - Kenya\Desktop\Ciheb\All_docs\Ciheb_script>

Hi @imamai

You’ve received a 200 response which is a correct response.

Thanks-@Gassim the status code : 201 is not met, the value is not created or updated on the entry screen but shows import successful. How can I go about that?

I assume http 200 is a success too. The problem with dhis2 is that it can be a partial success.

To be honest I generally don’t check the http status and you need to check the import summary json (especially when import tei/enrolllments/events)

note that the root “import summary” can say SUCCESS but the nested relationship might have been rejected for some reason.

1 Like

I completely agree with you @Stephan_Mestach, thanks! It does show SUCCESS and says that the object has been updated.

The response indicates that the object is not created but that it actually got updated which means that it was created before the request. Maybe it was created in your previous requests so when you tried again, it only got updated?