=== modified file 'src/docbkx/en/dhis2_user_man_web_api.xml' --- src/docbkx/en/dhis2_user_man_web_api.xml 2013-06-09 21:26:38 +0000 +++ src/docbkx/en/dhis2_user_man_web_api.xml 2013-07-05 09:55:38 +0000 @@ -1,5 +1,5 @@ - + Web API The Web API is a component which makes it possible for external systems to access and manipulate data stored in an instance of DHIS 2. More precisely, it provides a programmatic interface to a wide range of exposed data and service methods for applications such as third-party software clients, web portals and internal DHIS 2 modules. @@ -26,10 +26,8 @@ Authentication In order to interoperate with the Web API you will have to authenticate using Basic authentication. Basic authentication is a technique for clients to send login credentials over HTTP to a web server. Technically speaking, the username is appended with a colon and the password, Base64-encoded, prefixed Basic and supplied as the value of the Authorization HTTP header. More formally that is Authorization: Basic base64encode(username:password) An important note is that this authentication scheme provides no security since the username and password is sent in plain text and can be easily decoded. Using it is recommended only if the server is using SSL/TLS (HTTPS) to encrypt communication between itself and the client. Most DHIS 2 deployments typically use SSL today - consider it a hard requirement to provide secure interactions with the Web API. If you are building a form-based web application and want to authenticate using a web form - you can have the form send a POST request to the login endpoint in DHIS which is /dhis-web-commons-security/login.action . Two request parameters, - j_username and j_password, containing the username and password in clear-text respectively, are + you can have the form send a POST request to the login endpoint in DHIS which is /dhis-web-commons-security/login.action . Two request parameters, + j_username and j_password, containing the username and password in clear-text respectively, are expected. The browser will then receive a cookie which will be used for authentication for subsequent requests. You can verify and get information about the currently authenticated user by making a GET @@ -130,12 +128,10 @@
Browsing the Web API - The entry point for browsing the Web API is /api/resources. This resource provide links to all available resources. Four + The entry point for browsing the Web API is /api/resources. This resource provide links to all available resources. Four resource representation formats are consistently available for all resources: HTML, XML, JSON and JSONP. Some resources will have other formats available, like MS Excel, PDF, CSV and PNG. - To explore the API from a web browser, navigate to the /api/resources entry point and follow the links to your desired resource, for + To explore the API from a web browser, navigate to the /api/resources entry point and follow the links to your desired resource, for instance /api/resources/dataElements. For all resources which return a list of elements certain query parameters can be used to modify the response: @@ -197,15 +193,13 @@ using the query path variable. In this example we query for all data elements with the word "anaemia" in the name: /api/dataElements/query/anaemia - You can search for elements through the id, name and code property using the search path variable. In this example we search for a data element + You can search for elements through the id, name and code property using the search path variable. In this example we search for a data element with the word "DE_34" as id, name or code:/api/dataElements/search/DE_34
Working with the meta-data API - The meta-data resource can be accessed at /api/metaData. This resource lets you read and write the full set of meta-data. + The meta-data resource can be accessed at /api/metaData. This resource lets you read and write the full set of meta-data. This section will give a basic introduction to working with this API. For specific synchronization issues, please see the integration chapter. By default, interacting with /api/metaData using the @@ -293,8 +287,7 @@ Example: Get a filtered set of meta-data that was updated since August 1 2012 - As described in the last section, there is a number of options you can apply to /api/metaData to give you a filtered view. The use-case we will be + As described in the last section, there is a number of options you can apply to /api/metaData to give you a filtered view. The use-case we will be looking into here is where you want a nightly job that synchronizes organisation units. We will be using cURL as the HTTP client.curl -H "Accept: application/xml" -u admin:district "http://apps.dhis2.org/demo/api/metaData?assumeTrue=false&organisationUnits=true&lastUpdated=01/08/2012" @@ -315,14 +308,11 @@
Sending data values A common use-case for system integration is the need to send a set of data values from a - third-party system into DHIS. In this example we will use the DHIS 2 demo on as basis and we recommend that you follow the provided - links with a web browser while reading (log in with admin/district as username/password). We + third-party system into DHIS. In this example we will use the DHIS 2 demo on as basis and we recommend that you follow the provided + links with a web browser while reading (log in with admin/district as username/password). We assume that we have collected case-based data using a simple software client running on mobile phones for the Mortality <5 years data set in the - community of Ngelehun CHC (in Badjia chiefdom, Bo district) for the month + community of Ngelehun CHC (in Badjia chiefdom, Bo district) for the month of January 2012. We have now aggregated our data into a statistical report and want to send that data to the national DHIS 2 instance. The entry point for the Web API running on the demo instance is . The entry point provides a convenient HTML page with links to all of the available resources in the Web API. The resource which is most appropriate for our purpose of sending data values is the dataValueSets resource. A data value set represents a set of data values which have a logical relationship, usually from being captured off the same data entry form. We follow the link to the HTML representation which will take us to . The default representation is a HTML page which provides us with useful instructions on how to interact with this resources. It tells us that we can use the POST verb to send values using a XML format defined by the http://dhis2.org/schema/dxf/2.0 namespace: @@ -334,10 +324,8 @@ </dataValueSet> Note: We have omitted the categoryOptionCombo attribute as it is optional and not needed for this example. Please refer to the date and period section above for time formats. From the example we can see that we need to identify the period, the data set, the org unit (facility) and the data elements for which to report. The dataValueSets resource description tells us that the identifier for monthly periods should be on the format yyyyMM which means that we will use 201201 for January 2012. - To obtain the identifier for the data set we return to the entry point at and follow the embedded link pointing at the - dataSets resource located at . From there we find and follow the link to + To obtain the identifier for the data set we return to the entry point at and follow the embedded link pointing at the + dataSets resource located at . From there we find and follow the link to the Mortality < 5 years data set which leads us to . What we did was effectively to retrieve the HTML representation of our data set of interest, and from it we @@ -347,8 +335,7 @@ can follow these links and obtain the identifiers of the data elements. For brevity we will only report on three data elements: Measles with id f7n9E0hX8qk, Dysentery - with id Ix2HsbDMLea and Cholera with id eY5ehpbEsB7. + with id Ix2HsbDMLea and Cholera with id eY5ehpbEsB7. What remains is to get hold of the identifier of the facility (org unit). Again the dataSet representation conveniently provides link to org units which report on it so we search for Ngelehun CHC and follow the link to the HTML representation at , which tells us that the identifier of this org unit is DiszpKrYNg8. From our case-based data we assume that we have 12 cases of measles, 14 cases of dysentery and 16 cases of cholera. We have now gathered enough information to be able to put together the XML data value set message: <dataValueSet xmlns="http://dhis2.org/schema/dxf/2.0" dataSet="pBOMPrpg1QX" @@ -357,8 +344,7 @@ <dataValue dataElement="Ix2HsbDMLea" value="14"/> <dataValue dataElement="eY5ehpbEsB7" value="16"/> </dataValueSet> - To perform functional testing we will use the cURL tool () which provides an easy way of transferring data using HTTP. First we save the data value + To perform functional testing we will use the cURL tool () which provides an easy way of transferring data using HTTP. First we save the data value set XML content in a file called datavalueset.xml . From the directory where this file resides we invoke the following from the command line: curl -d @datavalueset.xml "http://apps.dhis2.org/demo/api/dataValueSets" -H "Content-Type:application/xml" -u admin:district -v @@ -373,12 +359,9 @@ to as anonymous events), single event with registration and multiple events with registration. Registration implies that the data is linked to a person which is identified using some sort of identifier. Only single event with no registration is supported in DXF 2 currently. - To send events to DHIS 2 you must interact with the events resource. The approach to sending events is similar to sending aggregate + To send events to DHIS 2 you must interact with the events resource. The approach to sending events is similar to sending aggregate data values. You will need a programId which can be looked - up using the programs resource, an organisationUnitId which can be looked up using the organisationUnits resource, and a list of valid data element identifiers which + up using the programs resource, an organisationUnitId which can be looked up using the organisationUnits resource, and a list of valid data element identifiers which can be looked up using the dataElements resource. An example payload in XML format where we send events from the "Inpatient morbidity and mortality" program for the "Ngelehun CHC" facility in the demo database can be seen @@ -392,8 +375,7 @@ <dataValue dataElement="msodh3rEMJa" value="2013-05-18" /> </dataValues> </event> - To perform some testing we can save the XML payload as a file called events.xml and send it as a POST request to the events resource in + To perform some testing we can save the XML payload as a file called events.xml and send it as a POST request to the events resource in the API using curl with the following command: curl -d @events.xml "http://apps.dhis2.org/demo/api/events" -H "Content-Type:application/xml" -u admin:district -v The same payload in JSON format looks like this: @@ -413,8 +395,7 @@ { "dataElement": "msodh3rEMJa", "value": "2013-05-18" } ] } - To send this you can save it to a file called events.json and use curl like this: + To send this you can save it to a file called events.json and use curl like this: curl -d @events.xml "localhost:8080/api/events" -H "Content-Type:application/json" -u admin:district -v We also support sending multiple events at the same time. A payload in XML format might look like this: (Caution: this format might change slightly in version 2.13) @@ -472,6 +453,15 @@ ] } ] } + (from 2.13) as part of the import summary you will also get the UID reference to the event + you just sent, together with a href element which points to the server location of this event. + Update: To update an existing event, the format of the + payload is the same, but the URL you are posting to must add the UID reference to the end of + the URL string and the request must be PUT. + curl -X PUT -d @updated_event.xml "localhost:8080/api/events/Abcas12322" -H "Content-Type:application/xml" -u admin:district -vcurl -X PUT -d @updated_event.json "localhost:8080/api/events/Abcas12322" -H "Content-Type:application/json" -u admin:district -v + Delete: To delete an existing event, all you need is to + send a DELETE request with a UID reference to the server you are + using.curl -X DELETE "localhost:8080/api/events/Abcas12322" -u admin:district -v The table below describes the meaning of each element. Most elements should be fairly self-explanatory. @@ -545,14 +535,14 @@ dataElementId string true - + UID of data element value string true - + Data value or measure for this event @@ -765,11 +755,9 @@ curl "http://apps.dhis2.org/demo/api/dataValueSets?dataSet=pBOMPrpg1QX&dataSet=BfMAe6Itzgt&startDate=2013-01-01&endDate=2013-01-31&orgUnit=YuQRtpLP10I&orgUnit=vWbkYPRmKyS&children=true" -H "Accept:application/xml" -u admin:district -v - You can get the response in xml and csv format. You can indicate which response format you prefer + You can get the response in xml and csv format. You can indicate which response format you prefer through the Accept HTTP header like in the example above. - For xml you use application/xml; for csv you use application/csv. + For xml you use application/xml; for csv you use application/csv.
Writing and reading messages @@ -1381,8 +1369,7 @@ delimited values. As an example, the following command would retrieve XML data for the SQL view defined above.curl "http://apps.dhis2.org/dev/api/sqlViews/dI68mLkP1wN/data.csv" -u admin:district -v - You can do simple filtering on the columns in the result set by appending criteria query parameters to the URL, using the column names and + You can do simple filtering on the columns in the result set by appending criteria query parameters to the URL, using the column names and filter values separated by columns as parameter values, on the following format: /data?criteria=col1:value1&criteria=col2:value2 As an example, to filter the SQL view result set above to only return organisation units @@ -1391,8 +1378,7 @@
Analytics - To access analytical, aggregated data in DHIS 2 you can work with the analytics resource. The analytics resource is powerful as it lets + To access analytical, aggregated data in DHIS 2 you can work with the analytics resource. The analytics resource is powerful as it lets you query and retrieve data aggregated along all available data dimensions. For instance, you can ask the analytics resource to provide the aggregated data values for a set of data elements, periods and organisation units. Also, you can retrieve the aggregated data for a @@ -1403,8 +1389,7 @@ dimension. You can dynamically add dimensions through categories, data element group sets and organisation unit group sets. The table below displays the available data dimensions in DHIS 2. Each data dimension has a corresponding dimension - identifier, and each dimension can have a set of dimension items: + identifier, and each dimension can have a set of dimension items: @@ -1467,8 +1452,7 @@ dimensions when designing analytics queries. You can get a complete list of dynamic dimensions by visiting this URL in the Web API:api/dimensions - The base URL to the analytics resource is api/analytics. To request specific dimensions and dimension items you can use a + The base URL to the analytics resource is api/analytics. To request specific dimensions and dimension items you can use a query string on the following format, where dim-id and dim-item should be substituted with real values: api/analytics?dimension=dim-id:dim-item;dim-item&dimension=dim-id:dim-item;dim-item @@ -1636,8 +1620,7 @@ In order to have the analytics resource generate the data in the shape of a ready-made table, you can provide the tableLayout parameter with true as value. Instead of generating a plain, normalized data source, the analytics resource - will now generate the data in table layout. You can use the columns and rows parameters with dimension + will now generate the data in table layout. You can use the columns and rows parameters with dimension identifiers separated by semi-colons as values to indicate which ones to use as table columns and rows. The column and rows dimensions must be present as a data dimension in the query (not a filter). Such a request can look like this: @@ -1673,8 +1656,7 @@ As an example, to request an analytics response in XML format you can use the following URL: api/analytics.xml?dimension=dx:fbfJHSPpUQD&dimension=pe:2012&dimension=ou:O6uvpzGd5pu;lc3eMKXaEfw - The analytics responses must be retrieved using the HTTP GET method. This allows for direct linking to analytics responses from Web + The analytics responses must be retrieved using the HTTP GET method. This allows for direct linking to analytics responses from Web pages as well as other HTTP-enabled clients. To do functional testing we can use the cURL library. By executing this command against the demo database you will get an analytics response in JSON format: @@ -1740,18 +1722,12 @@ "width": 3 } - The response represents a table of dimensional data. The headers array gives an overview of which columns are included in the table and + The response represents a table of dimensional data. The headers array gives an overview of which columns are included in the table and what the columns contain. The column property shows the column dimension identifier, or if the column contains measures, the word "Value". The - meta property is true if the column contains dimension items or false if the column contains a measure (aggregated data values). The name property is similar to the column property, except it - displays "value" in case the column contains a measure. The type property indicates the Java class type of the column values. - The height and width properties indicate how many data columns and rows are contained in the + meta property is true if the column contains dimension items or false if the column contains a measure (aggregated data values). The name property is similar to the column property, except it + displays "value" in case the column contains a measure. The type property indicates the Java class type of the column values. + The height and width properties indicate how many data columns and rows are contained in the response, respectively. The metaData periods property contains a unique, ordered list of the periods included in the response. The metaData @@ -1830,8 +1806,7 @@ FRED API DHIS 2 from version 2.11 implements support for the current draft of the FRED API version 1.0. The project defines itself as open standard for sharing and updating - health facility data. The full specification, including representation format and basic usage, can be found at http://facilityregistry.org/. + health facility data. The full specification, including representation format and basic usage, can be found at http://facilityregistry.org/. Since version 1.0 is not finalized there are parts of the current specification that has not been implemented as we found it not to be in a stable enough state. Most notably we do not currently support sorting (we do however sort on name by default) and filtering of facilities.