Category: 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.

Kong: Name resolution failed

Sometimes when calling a service on kong you might get a response like this

HTTP/1.1 503 Service Temporarily Unavailable
Connection: keep-alive
Content-Length: 36
Content-Type: application/json; charset=utf-8
Date: Thu, 02 Apr 2020 09:28:06 GMT
Server: kong/2.0.1
X-Kong-Response-Latency: 12

{
    "message": "name resolution failed"
}

What does it mean?

Kong cannot access the upstream

Make sure the corret protocol http or https is used.
Also if the upstream's SSL cannot be verified then

Kong: Failure to get a peer from the ring-balancer

Ever seen this error when calling kong:


HTTP/1.1 503 Service Temporarily Unavailable
Connection: keep-alive
Content-Length: 59
Content-Type: application/json; charset=utf-8
Date: Wed, 28 Aug 2019 08:08:01 GMT
Server: kong/0.14.1

{
    "message": "failure to get a peer from the ring-balancer"
}

What does it mean?

We it cannot access any of the upstreams or all of the upstreams are unhealthy.

It pretty much means Kong could connect to none of the backend services in the upstream pool.

Remember to ensure at least one upstream is healthy, otherwise unhealthy upstreams are ignored.

kong-upstream-unhealthy

Sources: