Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

This blog post is about calling a remote REST Service, e.g. some 3rd Party Application, which is publishing its data via a REST API.

This could be done with the VDS, executing a HTTP Request against this REST Service.


It is also possible to perform this inside a JavaScript, which will be executed by the IdM runtime directly, without the need to set up a VDS inside your landscape.

Unfortunately, the used Rhino JavaScript Engine used inside IdM is not able to perform AJAX calls directly, so we have to do this via Java (Thanks kai.ullrich for the hint with "Scripting Java inside JavaScript").

Below you find some example code.

Cheers, Jannis

// Main function: doTheAjax

function doTheAjax(Par){

    // import all needed Java Classes

    importClass(Packages.java.net.HttpURLConnection);

    importClass(Packages.java.net.URL);

    importClass(Packages.java.io.DataOutputStream);

    importClass(Packages.java.io.InputStreamReader);

    importClass(Packages.java.io.BufferedReader);

    importClass(Packages.java.lang.StringBuffer);

    importClass(Packages.java.lang.Integer);

    // variables used for the connection, best to import them via the table in a ToGeneric Pass

    var urlString = "http://host:port/rest_api";

    var urlParameters = "attribute=value";

    var httpMethod = "POST"; //or GET

    var username = "administrator";

    var password = "abcd1234";

    var encoding = uToBase64(username + ":" + password);

    // In case of GET, the url parameters have to be added to the URL

    if (httpMethod == "GET"){

        var url = new URL(urlString + "?" + urlParameters);

        var connection = url.openConnection();

        connection.setRequestProperty("Authorization", "Basic " + encoding);

        connection.setRequestMethod(httpMethod);

    }

    // In case of POST, the url parameters have to be transfered inside the body

    if (httpMethod == "POST"){

        // open the connection

        var url = new URL(urlString);

        var connection = url.openConnection();

        connection.setRequestProperty("Authorization", "Basic " + encoding);

        connection.setRequestMethod(httpMethod);

        connection.setDoOutput(true);

        connection.setDoInput(true);

        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        connection.setRequestProperty("charset", "utf-8");

        connection.setRequestProperty("X-Requested-With", "XMLHttpRequest");

        //connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));

        connection.setUseCaches(false);

        var os = new DataOutputStream(connection.getOutputStream());

        os.writeBytes(urlParameters);

        os.flush();

        os.close();

    }

    //get the result and print it out

    var responseCode = connection.getResponseCode();

    var is = connection.getInputStream();

    var isr = new InputStreamReader(is);

    var br = new BufferedReader(isr);

    var response = new StringBuffer();

    var line;

    while ((line = br.readLine()) != null) {

        response.append(line);

    }

    br.close();

    uWarning("Sending " + httpMethod + " Request to URL: " + urlString);

    uWarning("Response Code: " + responseCode);

    uWarning("Response: " + response.toString());

    connection.disconnect();

}

12 Comments