Java script to extract value from query parameters and pass null value if query parameters is absent

Hi Experts,

Requirement: Extract values from Query parameters and pass them to target URL using place holder.

for example Source URL: 

https://ab-btp-dev-apiportal.apimanagement.us21.hana.ondemand.com:443/org/v1/api/orgdata?docnum=DV-2014-00005&pagesize=100&datasource=WS_DEVIATION

Where docnum and datasource are query parameters as shown in above URL.

Target URL should look like this :

http://utsqes.ghd.com:8080/Dev/rest/v1/datasources/{ds_name}/execute?NUMBER$PARAM='{num}'&pagesize=1...

I am able to acheive the above requirement to generate target URL dynamically by following below steps if all query parameters are mandatory [atleast query parameters are populated with NULL value].

I followed below steps to acheive this requirement.

Steps 1: Created new Proxy.

Use target URL as

http://utsqes.ghd.com:8080/Dev/rest/v1/datasources

Steps 2:

Edit previously created API Proxy and give URL as below:

http://utsqes.ghd.com:8080/Dev/rest/v1/datasources/{ds_name}/execute?NUMBER$PARAM='{num}'&pagesize=1...And

Edited the API policy, add JS in Postflow under ProxyEndPoint, write the code as per screenshot

Nithin_26_0-1674486825610.png

 

<!-- this policy allows us to execute java script code during execution of an API Proxy -->

<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" xmlns='http://www.sap.com/apimgmt'>

              <!-- contains the name of the main code file -->

              <ResourceURL>jsc://setTargetPath.js</ResourceURL>

</Javascript>

Now create new JS and write code as per below screenshot and details:

Nithin_26_1-1674486825617.png

Below is the JS developed.

var ds_name = context.getVariable('request.queryparam.ds_name');

context.setVariable("target.copy.pathsuffix", true);

context.setVariable('ds_name', ds_name);

var num = context.getVariable('request.queryparam.num');

context.setVariable("target.copy.pathsuffix", true);

context.setVariable('num', num);

 

Additional requirement : If Query parameters itself is absent not even query parameters are present in source URL , then target URL should be passed with null value.

Meaning, source URL will look like below.

https://ab-btp-dev-apiportal.apimanagement.us21.hana.ondemand.com:443/org/v1/api/orgdata?&pagesize=1...datasource=WS_DEVIATION

[where docnum query parameter itself is absent in source URL.In this case null value should be passed to corresponding placeholder on target URL]

Please help to acheive this requirement.

Thanks,

Nithin.

 

 

 

0 1 435
1 REPLY 1

Not quite sure you need to use JavaScript, but you may be able to use an Assign Message which would be a bit easier to manage.

For example, in the Target Flow:

 

<AssignMessage continueOnError="false" enabled="true" name="AM-set-target-params">
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignVariable>
        <Name>qp_one</Name>
        <Value>not-set</Value>
        <Ref>request.queryparam.one</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>qp_two</Name>
        <Value>null</Value>
        <Ref>request.queryparam.two</Ref>
    </AssignVariable>
    <AssignVariable>
        <Name>qp_three</Name>
        <Ref>request.queryparam.three</Ref>
    </AssignVariable>
    <Remove>
        <QueryParams/>
    </Remove>
    <Set>
        <QueryParams>
            <QueryParam name="target-one">{qp_one}</QueryParam>
            <QueryParam name="target-two">{qp_two}</QueryParam>
            <QueryParam name="target-three">{qp_three}</QueryParam>
        </QueryParams>
    </Set>
</AssignMessage>

 

In the AssignVariable elements above is the query param is not specified, the policy will assign the default "Value" to the variable "Name". The "Remove" element removes all incoming query params and new values are "Set".

Hope that helps or gives you other options.