How to extract all the queryaprams names and values in javascript.
I tried below snippent but unable to fetch value of that queryparam dynmically.
var queryParams = context.getVariable('request.queryparams.names'); var count = context.getVariable('request.queryparams.count'); var queryString = context.getVariable('request.querystring'); if(count > 0){ var queryParamsArray = queryParams.toArray(); var first = queryParamsArray[0].toString(); print(context.getVariable('request.queryparam.type')); print(queryParams[first]); print(context.getVariable('request.queryparam['first']')); }else{ context.setVariable('badQuery',"false"); }
the print statement is not prinitng value of queryparam type. Any help would be really appreciated
Solved! Go to Solution.
Here you go,
var count = context.getVariable('request.queryparams.count'); if (count > 0) { var queryString = context.getVariable('request.querystring'); var queryParamsArray = queryString.split("&"); var queryParamsArrayLength = queryParamsArray.length; for (var i = 0; i < queryParamsArrayLength; i++) { var queryParamArray = queryParamsArray[i].split("="); print("Query Param Key :" + queryParamArray[0]); print("Query Param Value :" + queryParamArray[1]); } }else{ context.setVariable('badQuery',"false"); }
@akshay.anand9 , Can you add a sample URI & What exactly you would like to see as an output in the print statements ? Below code is not a valid syntax in Apigee,
print(context.getVariable('request.queryparam['first']'));
Here you go,
var count = context.getVariable('request.queryparams.count'); if (count > 0) { var queryString = context.getVariable('request.querystring'); var queryParamsArray = queryString.split("&"); var queryParamsArrayLength = queryParamsArray.length; for (var i = 0; i < queryParamsArrayLength; i++) { var queryParamArray = queryParamsArray[i].split("="); print("Query Param Key :" + queryParamArray[0]); print("Query Param Value :" + queryParamArray[1]); } }else{ context.setVariable('badQuery',"false"); }
Mines like this. Just for reference, hehe.
var url = context.getVariable('response.header.Location'); function getUrlParameter(name) { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var results = regex.exec(url); return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); } context.setVariable('ExtractedAuthCode',getUrlParameter('code'));
where getUrlParamter will be the paramter to get and url is the URI.
User | Count |
---|---|
1 | |
1 | |
1 | |
1 | |
1 |