Extract values from source system and Pass Query parameters values to Target URL

Hi Experts,

We are working on API to pass some 10 query parameters values from source to target URL.

We have source URL something like below As you can see there are around 10 query parameters coming from Source system in URL pasted below.

https://<host>/org/v1/api/compdata/vendor_profile?companyno=06598&companyname=&siteno=&sitename=&address=&city=&state=&postal=&country=&pagesize=. I am using place holder and java script to pass values from query parameters passed from source to target URL.Target URL looks something like below.

http://<host>:<port>/OrgDev/rest/v1/datasources/VENDOR_PROFILE/execute?COUNTRY$PARAM='{country}'&COMPANY_NAME$PARAM='{companyname}'&COMPANY_NUMBER$PARAM='{companyno}'&SSP_NUMBER$PARAM='{siteno}'&SSP_NAME$PARAM='{sitename}'&SSP_ADDRESS$PARAM='{address}'&SSP_CITY_COUNTY$PARAM='{city}'&SSP_STATE$PARAM='{state}'&SSP_POSTAL_CODE$PARAM='{postal}'&pagesize='{pagesize}:

Javascript used as below mentioned with getVariable and request.queryparam.{queryparam}

I pasted Javascript code for only a few parameters since same 3 lines code applies for others also.

var country = context.getVariable('request.queryparam.country');
context.setVariable("target.copy.pathsuffix", true);
context.setVariable('country', country);

var companyname = context.getVariable('request.queryparam.companyname');
context.setVariable("target.copy.pathsuffix", true);
context.setVariable('companyname', companyname);

var companyno = context.getVariable('request.queryparam.companyno');
context.setVariable("target.copy.pathsuffix", true);
context.setVariable('companyno', companyno);

.................................................... same 3 lines code for all query parameters.

My configuration works completely fine when there are only 5 or 6 query parameters but does not work there are more query parameters from source like in this case where there are 10 query parameters. Even API is not getting deployed when I give above target URL in bold as per below screenshot. Looks like there is limitation of 256 characters to be used in Target URL when I try to deploy the API itself.

I get below error when I try to deploy the API like unable to update API and when I remove some query parameters to adjust the characters count to 256 or less then API is getting deployed successfully.

 

Nithin_26_1-1677175204638.png

The above configuration works fine when there are only less query parameters.

I also used Assign Message policy  in ProxyEndpoint - postflow to acheive this requirement but even that is not working. Please find below policy used.

Assign message policy looks like below in my case since there are multiple query parameters.

<AssignMessage async="false" continueOnError="false" enabled="true"> <Add>
<QueryParams>
<QueryParam name="SSP_COUNTRY$PARAM">{request.queryparam.country}</QueryParam>
<QueryParam name="COMPANY_NAME$PARAM">{request.queryparam.companyname}</QueryParam>
<QueryParam name="COMPANY_NUMBER$PARAM">{request.queryparam.companyno}</QueryParam>
<QueryParam name="SSP_NUMBER$PARAM">{request.queryparam.siteno}</QueryParam>
<QueryParam name="SSP_NAME$PARAM">{request.queryparam.sitename}</QueryParam>
<QueryParam name="SSP_ADDRESS$PARAM">{request.queryparam.address}</QueryParam>
<QueryParam name="SSP_CITY_COUNTY$PARAM">{request.queryparam.city}</QueryParam>
<QueryParam name="SSP_STATE$PARAM">{request.queryparam.state}</QueryParam>
<QueryParam name="SSP_POSTAL_CODE$PARAM">{request.queryparam.postal}</QueryParam>
<QueryParam name="pagesize">{request.queryparam.pagesize}</QueryParam>
</QueryParams>
</Add>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="request">request</AssignTo>
</AssignMessage>

It is throwing error in postman as shown below.

Nithin_26_2-1677176048894.png

I am getting below error on debub mode, kindly help to resolve this.

Nithin_26_3-1677177345041.png

I am getting below errors on Debug mode.

Nithin_26_7-1677177716085.png

Marked step on above debug mode is having below one of the parameters.

Nithin_26_6-1677177615241.png

Could you please guide me if we need to use Javascript to buid query paramters on Target URL or it could be possible by even Assign Message policy also. Do we need to setback URI using Javascript to acheive this requirement , Kindly guide to acheive this.

Thank you!

Regards!

 

 

 

 

 

0 4 390
4 REPLIES 4

If you are receiving from source system you can just propagate the query params with out any extract/assignment.

 Kindly do below..

1. Disable copy path suffix in target pre-flow

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="DisableCopyPath">
<DisplayName>DisableCopyPath</DisplayName>
<AssignVariable>
<Name>target.copy.pathsuffix</Name>
<Value>false</Value>
</AssignVariable>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

2. Assign target url & append the pathsuffix & query params.. (assuming you are loaded target url into kvm & extracted/assigned to targetUrl).

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="AssignTarget">
<DisplayName>AssignTarget</DisplayName>
<Properties/>
<AssignVariable>
<Name>target.url</Name>
<Template>{targetUrl}{proxy.pathsuffix}?{request.querystring}</Template>
</AssignVariable>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

reference on variables - https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#request

 

Hello,

Thank you for your response, please note that name of query paramters from source system are different than that of target system, for example from source we get "country" and in target it should be "SSP_COUNTRY$PARAM". Also please note that order in which we get them not same. Can we do this using below in Javascript? Kindly suggest.

Nithin_26_0-1677224936082.png

 

Thanks,

Nithin.


@Nithin_26 wrote:

Hi Experts,

We are working on API to pass some 10 query parameters values from source to target URL.

We have source URL something like below As you can see there are around 10 query parameters coming from Source system in URL pasted below.

https://<host>/org/v1/api/compdata/vendor_profile?companyno=06598&companyname=&siteno=&sitename=&address=&city=&state=&postal=&country=&pagesize=. I am using place holder and java script to pass values from query parameters passed from source to target URL.Target URL looks something like below.

http://<host>:<port>/OrgDev/rest/v1/datasources/VENDOR_PROFILE/execute?COUNTRY$PARAM='{country}'&COMPANY_NAME$PARAM='{companyname}'&COMPANY_NUMBER$PARAM='{companyno}'&SSP_NUMBER$PARAM='{siteno}'&SSP_NAME$PARAM='{sitename}'&SSP_ADDRESS$PARAM='{address}'&SSP_CITY_COUNTY$PARAM='{city}'&SSP_STATE$PARAM='{state}'&SSP_POSTAL_CODE$PARAM='{postal}'&pagesize='{pagesize}:

Javascript used as below mentioned with getVariable and request.queryparam.{queryparam}

I pasted Javascript code for only a few parameters since same 3 lines code applies for others also.

var country = context.getVariable('request.queryparam.country');
context.setVariable("target.copy.pathsuffix", true);
context.setVariable('country', country);

var companyname = context.getVariable('request.queryparam.companyname');
context.setVariable("target.copy.pathsuffix", true);
context.setVariable('companyname', companyname);

var companyno = context.getVariable('request.queryparam.companyno');
context.setVariable("target.copy.pathsuffix", true);
context.setVariable('companyno', companyno);

.................................................... same 3 lines code for all query parameters.

My configuration works completely fine when there are only 5 or 6 query parameters but does not work there are more query parameters from source like in this case where there are 10 query parameters. Even API is not getting deployed when I give above target URL in bold as per below screenshot. Looks like there is limitation of 256 characters to be used in Target URL when I try to deploy the API itself.

I get below error when I try to deploy the API like unable to update API and when I remove some query parameters to adjust the characters count to 256 or less then API is getting deployed successfully.

 

Nithin_26_1-1677175204638.png

The above configuration works fine when there are only less query parameters.

I also used Assign Message policy  in ProxyEndpoint - postflow to acheive this requirement but even that is not working. Please find below policy used.

Assign message policy looks like below in my case since there are multiple query parameters.

<AssignMessage async="false" continueOnError="false" enabled="true"> <Add>
<QueryParams>
<QueryParam name="SSP_COUNTRY$PARAM">{request.queryparam.country}</QueryParam>
<QueryParam name="COMPANY_NAME$PARAM">{request.queryparam.companyname}</QueryParam>
<QueryParam name="COMPANY_NUMBER$PARAM">{request.queryparam.companyno}</QueryParam>
<QueryParam name="SSP_NUMBER$PARAM">{request.queryparam.siteno}</QueryParam>
<QueryParam name="SSP_NAME$PARAM">{request.queryparam.sitename}</QueryParam>
<QueryParam name="SSP_ADDRESS$PARAM">{request.queryparam.address}</QueryParam>
<QueryParam name="SSP_CITY_COUNTY$PARAM">{request.queryparam.city}</QueryParam>
<QueryParam name="SSP_STATE$PARAM">{request.queryparam.state}</QueryParam>
<QueryParam name="SSP_POSTAL_CODE$PARAM">{request.queryparam.postal}</QueryParam>
<QueryParam name="pagesize">{request.queryparam.pagesize}</QueryParam>
</QueryParams>
</Add>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="request">request</AssignTo>
</AssignMessage>

It is throwing error in postman as shown below.

Nithin_26_2-1677176048894.png

I am getting below error on debub mode, kindly help to resolve this.

Nithin_26_3-1677177345041.png

I am getting below errors on Debug mode.

Nithin_26_7-1677177716085.png

Marked step on above debug mode is having below one of the parameters.

Nithin_26_6-1677177615241.png

Could you please guide me if we need to use Javascript to buid query paramters on Target URL or it could be possible by even Assign Message policy also. Do we need to setback URI using Javascript to acheive this requirement , Kindly guide to acheive this.

Thank you!

Regards!

 

 

 

 

 


No issues while testing locally but may be try using below snippet & validate. Again its a sample code and can be better to read the target base url from kvm..

try {
var targetUrl = "https://test.org/OrgDev/rest/v1/datasources/VENDOR_PROFILE/execute?";
var queryParams = context.getVariable("request.querystring");
if (queryParams) {
var queryParamNames = {
"companyno": "COMPANY_NUMBER",
"companyname": "COMPANY_NAME",
"siteno": "SITE_NUM",
"sitename": "SITE_NAME",
"address": "ADDRESS",
"city": "CTY",
"state": "STATE",
"postal": "POSTAL_CODE",
"country": "COUNTRY",
"pagesize": "PAGE_SIZE"
};
queryParams.split("&").forEach(function(param) {
var paramNameValue = param.split("=");
var paramName = decodeURIComponent(paramNameValue[0]);
var paramValue = decodeURIComponent(paramNameValue[1]);
var queryParamName = queryParamNames[paramName];
if (queryParamName !== undefined) {
targetUrl += queryParamName + "=" + paramValue + "&";
}
});
targetUrl = targetUrl.slice(0, -1); // remove last "&" character
}
context.setVariable("target.url", targetUrl);
} catch (error) {
context.setVariable("debug", error);
throw error;
}

I was able to use your AssignMessage policy on Apigee Edge and it duplicates the query parameter values into the new parameters as expected.

Can you check what is getting sent from trace in Apigee? Do you have any policies earlier that is manipulating the request object and potentially clearing it for example?