Skip to Content

Consume Http destination in xsjs file in XSA Advanced Application

Hello all!

I'm developing an xsa advanced application and I would like to consume an API through an xsjs file, but I find myself unable to find the right way to do it.

I read that it is possible to create an http destination, but I can't make it to work.

Does anybody know how to consume an API through an xsjs file?

I'm using Hana express edition with its webIDE.

Thanks a lot!

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Mar 19 at 04:14 PM

    Hi,

    A similar functionality is part of the openSAP HANA XSA SPS02 course excercises ... in there an HTTP call is made via xsjs ...

    Basically you need to register an user provided service with the HTTP settings, define the resource in the mta and require it in your xsjs module. Then you can access it in your code:

    var dest = $.net.http.readDestination("sap.hana.democontent.epm.services", "images.SDS");     
    var client = new $.net.http.Client();     
    var req = new $.web.WebRequest($.net.http.GET, search);     
    client.request(req, dest);     
    var response = client.getResponse();
    ...

    The exercises can be found on GitHub at HANA XSA SPS02 - have a look at the ZIP file sap.hana.democontent.epm.services.zip and in there at the file 'outboundTest.xsjs'.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi David,

      I had a look into the openSAP content and there is indeed an xshttpdest file as well ( with redundant info ). I am not sure either why both are required ... but ofc the XSA provides XSJS backward compatibility so probably both the service and the file need to exist to keep both CloudFoundry and XSJS happy (or at least that is my guess ).

      Have a look at the following GIT branch ... it contains all the code of the openSAP course ...

      SAP GIT

      Under '/core_js/lib/sap/hana/democontent/epm/services/' you can find the destination file and the xsjs 'outboundtest' which perform the external HTTP call

      Also the mta.yaml file in the root will show you which parameters to fill in ...

      Hope this helps,

      Kr,

      Steven

  • Apr 16 at 07:07 AM

    Hi David,

    Did you manage to solve the problem with the HTTP destination ?

    I stumbled upon the following documentation within the 'readme' documentation of the standard sap node js 'xsjs' module - so bascially it confirms that the UP service properties are used first - with a fallback towards the design time file.

    ## Destinations support
    
    #### Via user provided services
    By default the compatibility layer supports destinations configuration via user provided services. The destination name (the repo resource id, e.g. package + '.' + xshttpdest name) is matched to service name.
    
    Example content of VCAP_SERVICES:
    
    ```js
    "VCAP_SERVICES": {
      "user-provided": [
        {
          "label": "user-provided",
          "name": "foobar.httpdest.mydest",
          "credentials": {
            "host": "some.host",
            "port": 8088,
            "username": "user",
            "password": "secret"
          }
        }
      ]
    }
    ```
    Example usage in XSJS code:
    ```js
    var destination = $.net.http.readDestination('foobar.httpdest', 'mydest');
    ```
    If there is no service in VCAP_SERVICES with same name as the destination requested, an exception is thrown.
    
    When destination is read the content of the design time descriptor is merged with the properties provided in the user provided service. Property values of the UP service override DT descriptor values.
    
    

    Add comment
    10|10000 characters needed characters exceeded