am not a regex expert - but i had a sample like this -
var re = /http\:\/\/54\.144\.216\.0\:9000\/temp/g;
str = 'http://the-replace-url';
var rc = context.getVariable("response.content");
var newstr = rc.replace(re, str);
context.setVariable("response.content", newstr);
I think script is the way to go. Allows you great flexibility and performs well. For simple static types of replacements I think Mukundha’s answer is just fine. I often run into the need to do something more dynamic, so I’ll try to parameterize the replacement a bit more and make use of a URL library like URI.js to make things less error-prone. For example, here’s a script I’ve used to make sure that any URL referencing the target is replaced with the proxy endpoint. This example only tries to rewrite target basepath + optional uuid. You could make this more or less generic as needed. I’m setting variables earlier in the flow to ensure my replacement URLs match precisely how the caller hit the endpoint. The URI library is probably overkill in this simple example but it makes it easier if I later need to go in and start mucking about with paths in interesting ways. Also this was sort of quick and dirty, so some verification still needs to be done to ensure all URLs would be handled correctly.
// Used to rewrite URLs in the response to route through same proxy.
// URLRewrite policy.
var pscheme = context.getVariable('pscheme')
var phost = context.getVariable('phost')
var pbase = context.getVariable('pbase')
var tscheme = context.getVariable('target.scheme')
var thost = context.getVariable('target.host')
var tbase = context.getVariable('target.basepath')
var uuid = context.getVariable("UUID")
if (uuid) {
pbase += '/' + uuid
}
var p = new URI({
protocol: pscheme,
hostname: phost,
path: pbase
})
var t = new URI({
protocol: tscheme,
hostname: thost,
path: tbase
})
var r = new RegExp(t.href(), "ig")
response.content = response.content.replace(r, p.href())