I have a json response as below. I want to replace the URL which exist in JSON response from
https://abc.oktapreview.com/api/
to
https://abc-dallas-ext-non-prod-ingress.ccc.abc.com/v1/conn/api/v1
[
{
"id": "00uafl8w1l5E5eeADcx7",
"status": "ACTIVE",
"created": "2017-05-10T15:34:18.000Z",
"activated": "2017-05-10T15:34:18.000Z",
"statusChanged": "2020-01-08T19:54:55.000Z",
"lastLogin": "2022-05-17T13:31:39.000Z",
"lastUpdated": "2022-03-18T12:03:30.000Z",
"passwordChanged": null,
"type": {
"id": "00uafl8w1l5E5eeADcx7"
},
"profile": {
"zzOpCo": "ABC",
"lastName": "Test",
"samAccountName": "TEST123",
"secondEmail": null,
"zzCountry": "US",
"login": "test123@mgd.abc.com",
"zzCity": "HBK",
"employeeNumber": "1234",
"email": "test.a@abc.com"
},
"credentials": {
"provider": {
"type": "ACTIVE_DIRECTORY",
"name": "abc.zx.com"
}
},
"_links": {
"self": {
"href": "https://abc.oktapreview.com/api/v1/users/00uafl8w1l5E5eeADcx7"
}
}
}
]
I think javascript is the way to achieve this. Do you have any sample code which can be used.
Solved! Go to Solution.
Will the url appear only in the _links.self.href field? In that case, try this
var c = JSON.parse(context.getVariable('response.content'));
var stringToReplace = 'abc.oktapreview.com/api';
var replacement = 'abc-dallas-whatever.abc.com/v1/whatever';
// assume c is array
c.forEach(function(item) {
if (item._links.self.href) {
item._links.self.href = item._links.self.href.replace(stringToReplace, replacement);
}
});
context.setVariable('response.content', JSON.stringify(c));
If there are other possible items in the links object that might have URLs, then you would need to generalize the logic in line 6 and 7.
Will the url appear only in the _links.self.href field? In that case, try this
var c = JSON.parse(context.getVariable('response.content'));
var stringToReplace = 'abc.oktapreview.com/api';
var replacement = 'abc-dallas-whatever.abc.com/v1/whatever';
// assume c is array
c.forEach(function(item) {
if (item._links.self.href) {
item._links.self.href = item._links.self.href.replace(stringToReplace, replacement);
}
});
context.setVariable('response.content', JSON.stringify(c));
If there are other possible items in the links object that might have URLs, then you would need to generalize the logic in line 6 and 7.
You can do programmatically in apigee but why not get proper response from backend rather spending time applying additional tricks before sending to client.
Assuming you exposed an endpoint (GET method) via apigee & backend is responding with incorrect links for pagination/HATEOAS & you want to apply a fix in apigee so that the client can continue calling proxy facing path & not backend path..
May be pass {request.header.X-Forwarded-Proto}://{request.header.Host}{message.path} & have it manipulated at backend and pass in the response..
keep it simple & clean. (a lot can be done in apigee but why not maintain logic in backend itself :))
yes, it's a good point. Sometimes the backend is not easily modified, so it's convenient to do it in Apigee. But I agree with you in general.