Handling REST API redirects

My company is currently evaluating Kong as a proxy between clients and a variety of REST API’s, with the idea of adding logging, auth, … in the middle.

I’m currently trying to set it up in a very minimal way, setting up just one test service (to an existing API), and am running into some problems to handle redirects.

In this situation (which I’m simplifying here) we we have three relevant hosts,

My example client code is in Python, using requests library, run from joris.mydomain.com.

When hitting testapi directly (i.e. not through Kong) on /api/v1/hello, this responds with a redirect to /api/v1/yo:

direct_url = 'http://testapi.mydomain.com:10056/api/v1/hello'
requests.get(direct_url, allow_redirects=False).headers

>> {'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '429', 'Location': 'http://testapi.mydomain.com:10056/api/v1/yo', 'Server': 'Werkzeug/0.14.1 Python/3.6.5', 'Date': 'Wed, 05 Sep 2018 16:48:50 GMT'}

which is as expected.

Now I’ve set up this API to be proxied by Kong:

curl -i -X POST --url http://localhost:8001/services/ --data 'name=testapi' --data 'host=http://testapi.mydomain.com' --data 'port=10056'
curl -i -X POST --url http://localhost:8001/services/testapi/routes/ --data 'paths[]=/test'

so I’m using path-based routing (’/test’), and ‘strip_path’ ends up being True by default (which I checked). If I hit this REST API on an endpoint that does not have redirects, through Kong, everything goes well. However, with redirects the behaviour seems to be undesirable.

proxied_url = 'http://kong.mydomain.com:8000/test/api/v1/hello'
requests.get(proxied_url, allow_redirects=False).headers

>> {'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '429', 'Connection': 'keep-alive', 'Location': 'http://kong.mydomain.com/api/v1/yo', 'Server': 'Werkzeug/0.14.1 Python/3.6.5', 'Date': 'Wed, 05 Sep 2018 14:39:15 GMT', 'X-Kong-Upstream-Latency': '61', 'X-Kong-Proxy-Latency': '4', 'Via': 'kong/0.14.1'}

I see two issues here:

  • The redirect path specifies /api/v1/yo, instead of /test/api/v1/yo, so the client-side is not correctly redirected.
  • The port (8000) is dropped from http://kong.mydomain.com, so a client gets redirected to port 80 by default, to which nothing is listening.

i.e. instead of http://kong.mydomain.com/api/v1/yo I’d have expected it to be http://kong.mydomain.com:8000/test/api/v1/yo

I am quite new to Kong (and nginx) - I only installed it earlier today - so it’s probable I am doing something anti-idiomatic or forget to set up certain functionality. I cannot immediately find a similar issue on this forum or elsewhere, though. All my settings are still default.

We’re keen on Kong as a solution, as it seems to provide exactly the right functionality - and would be interested in the Enterprise Edition if all goes well - so any help would be appreciated!

Best,
-Joris.

I approached this by writing a little fix_redirect plugin, that munges the response ‘Location’ header into a more appropriate form. This seems to be working fine for us.

Still interested to hear if this is the idiomatic solution, though!