Categories
Kong

Using Kong and Lua to change occurances of a url in the response body

Using the response transformer advanced plugin for kong, it looks like it is possible to replace urls in the body of an api.

The use case: you have an api that you have set to return hypermedia links as absolute urls to other resources.
The problem is that you have given the other party access to your api only by way of kong – an api gateway.
That means that the url’s in the response body point to an api they do not have direct access to.

For example your api responds with a body:

{
    "realms": "https://virtualisation.fixes.co.za/realms/",
    "records": "https://virtualisation.fixes.co.za/records/"
}

Your api gateway is https://api.fixes.co.za, so you want to update the response to convert to:

{
    "realms": "https://api.fixes.co.za/virtualisation/realms/",
    "records": "https://api.fixes.co.za/virtualisation/records/"
}

Is it possible?

My search term:

Lua function to replace occurances of base url in json body.

With the standard request transformer it does not look possible.

The closest would be config.replace.json which does List of property:value pairs. If and only if the parameter is already present, replace its old value with the new one. Ignored if the parameter is not already present.

I want to string replace anywhere https://virtualisation.fixes.co.za/ is found and replace it with https://api.fixes.co.za/virtualisation/.
In any value or nested value.

It looks like the only way to do it would be with a custom lua function in aconfig.transform.functions of the pay for response transformer advanced

So you have to pay for kong enterprise, learn lua and then apply the function. To achieve this.