Skip to content

Commit 635b36c

Browse files
author
igba.ujege
committed
Add rewrite hostname
1 parent 58f9471 commit 635b36c

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var response = context.getVariable("response.content");
2+
var json = JSON.parse(response);
3+
4+
if (json.place) {
5+
// Loop over each item in the search result array, re-writing the hostname to that of the initial request
6+
json.place.forEach(value => {
7+
var request_hostname = context.getVariable("request_hostname_env");
8+
// Annoyingly, Apigee's version of JS doesn't support regex look-behinds,
9+
// so we grab the env string including https:// prefix
10+
var hostname_regex = /(^https:\/\/([a-zA-Z\-]+\.)+)nhs\.uk/gm;
11+
var rewritten_url = value.url.replace(hostname_regex, "https://" + request_hostname);
12+
value.url = rewritten_url;
13+
// value.url = value.url.replace("api-version=1", "api-version=2");
14+
// value.url = value.url.replace("service-search", "service-search-api");
15+
});
16+
17+
// Update the response payload with the corrected urls
18+
context.setVariable("message.content", JSON.stringify(json));
19+
}

0 commit comments

Comments
 (0)