Hello all,
I am stuck with the code to use substring in javascript to extract the part of URL.
https://abc.com:9001/xyz/myrequest
I need to extract only /xyz
Thanks in advance.
Solved! Go to Solution.
Change to "print" , as I said refer to Apigee javascript samples and make adjustments.
print("Original address: "+url_add)
print(break_address(url_add))
Thank you everyone for your valuable time. Preferably I wanted to use only Javascript to reduce the redundancy of adding extra policies. I used the below script and it worked.
const supplyUrl = "https://my.supplier/supplier/tariffSelectionG_E5449168?t=randomChars" if(supplyUrl.includes('?')) { const url = supplyUrl.split('?') if(url.length > 0) { const tariff = url[0].split('supplier/') console.log('tariff:', tariff[1]) } }
Add the javascript in the blog post to a javascript policy to extract the parts of the url to derive what you need.
https://www.w3resource.com/javascript-exercises/javascript-basic-exercise-144.php
Thanks
I need to write javascript. The webpage doesn't return the script. If you know how to write the script, please let me know
Explain your use case. You will be using a javascript policy in your Apigee flow and the javascript source will have the following source (adjust to fir your requirements needs):
function break_address(url_add) {
var data = url_add.split("://")
var protocol = data[0];
data = data[1].split(".com");
var domain = data[0];
data = data[1].split("/");
if(data[1]){
return [protocol,domain,data[1]]
}
return [protocol,domain]
}
var url_add = "https://www.w3resource.com/javascript-exercises/"
console.log("Original address: "+url_add)
console.log(break_address(url_add))
console command is giving error as it is not defined.
Change to "print" , as I said refer to Apigee javascript samples and make adjustments.
print("Original address: "+url_add)
print(break_address(url_add))
You don't need javascript for this. In the URI you provided - https://abc.com:9001/xyz/myrequest - /xyz is the basepath of the proxy. You can reference this directly using a built-in variable `proxy.basepath`.
Also, if you want a suburi, you can use the Extract Variables policy - specifically the URIPath element. More on that at https://cloud.google.com/apigee/docs/api-platform/reference/policies/extract-variables-policy#uripat...
As example -
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="EV-suburi">
<DisplayName>EV-suburi</DisplayName>
<Properties/>
<URIPath>
<Pattern ignoreCase="false">/{suburi-0}</Pattern>
<Pattern ignoreCase="false">/{suburi-0}/{suburi-1}</Pattern>
<Pattern ignoreCase="false">/{suburi-0}/{suburi-1}/{suburi-2}</Pattern>
</URIPath>
<Variable name="proxy.basepath">
<Pattern>{basepath}</Pattern>
</Variable>
<Source clearPayload="false">request</Source>
</ExtractVariables>
Thank you everyone for your valuable time. Preferably I wanted to use only Javascript to reduce the redundancy of adding extra policies. I used the below script and it worked.
const supplyUrl = "https://my.supplier/supplier/tariffSelectionG_E5449168?t=randomChars" if(supplyUrl.includes('?')) { const url = supplyUrl.split('?') if(url.length > 0) { const tariff = url[0].split('supplier/') console.log('tariff:', tariff[1]) } }
That works, too!