Hi

I don't think we have any immediate plans to include this in the docs, the
idea about using cURL is that it's completely language neutral and any
language these days should give you the tools to interact with the API.

That said, a simple example for fetch the data elements from a DHIS2 server
would be (you need node-fetch installed):

const fetch = require( "node-fetch" );

const createAuthenticationHeader = (username, password) => {
  return "Basic " + new Buffer( username + ":" + password ).toString(
"base64" );
};

fetch( '
https://play.dhis2.org/dev/api/dataElements?paging=false&filter=domainType:eq:TRACKER',
{
  headers: {
    "Accept": "application/json",
    "Authorization": createAuthenticationHeader( 'admin', 'district' )
  }
} )
  .catch( err => console.error( err ) )
  .then( res => res.json() )
  .then( res => {
    console.log( res.dataElements.length );
  } )
  .catch( err => console.error( err ) );

If you would want to use an OAuth2 bearer token instead you would change
the Authorization header.

-- 
Morten Olav Hansen
Senior Engineer, DHIS 2
University of Oslo
http://www.dhis2.org

On Thu, Jul 27, 2017 at 10:36 PM, Knut Staring <knu...@gmail.com> wrote:

> This is a good question for the dev list.
>
> ---------- Forwarded message ----------
> From: Jesus Solano-Roman <asol...@broadinstitute.org>
> Date: Thu, Jul 27, 2017 at 3:15 PM
> Subject: [Dhis2-users] API calls using Node.js examples
> To: "dhis2-users lists.launchpad.net" <dhis2-us...@lists.launchpad.net>
>
>
> Hello all.
>
> I would like to know if someone could point out to examples or perhaps
> give an example of a typical Web API call done using Node.js
>
> The examples in the documentation are either using cURL or using the
> direct browser "endpoint", but usually do not take into account oAuth
> authentication (e.g. using a token instead of user credentials) and how to
> wrap all that into a Node.js request.
>
> In the use case of developing a web application, both the "Bearer token"
> and the following API calls should be done using Node (or say, PHP) and not
> cURL. So for instance, this example from the documentation should actually
> exist as a Node request:
>
> curl -H "Authorization: Bearer 07fc551c-806c-41a4-9a8c-10658bd15435"
> $SERVER/api/26/dataElements.json
>
> Any help would be greatly appreciated.
>
> Thanks,
>
>
> Antonio Solano.
> Broad Institute of MIT and Harvard, Boston, MA, USA.
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dhis2-users
> Post to     : dhis2-us...@lists.launchpad.net
> Unsubscribe : https://launchpad.net/~dhis2-users
> More help   : https://help.launchpad.net/ListHelp
>
>
>
>
> --
> Knut Staring
> Dept. of Informatics, University of Oslo
> Norway: +4791880522 <918%2080%20522>
> Skype: knutstar
> http://dhis2.org
>
> _______________________________________________
> Mailing list: https://launchpad.net/~dhis2-devs
> Post to     : dhis2-devs@lists.launchpad.net
> Unsubscribe : https://launchpad.net/~dhis2-devs
> More help   : https://help.launchpad.net/ListHelp
>
>
_______________________________________________
Mailing list: https://launchpad.net/~dhis2-devs
Post to     : dhis2-devs@lists.launchpad.net
Unsubscribe : https://launchpad.net/~dhis2-devs
More help   : https://help.launchpad.net/ListHelp

Reply via email to