# Handling REST API redirects

**URL:** <https://discuss.konghq.com/t/handling-rest-api-redirects/1825>\
**Category:** Questions\
**Created:** [September 5, 2018, 5:07pm UTC](https://discuss.konghq.com/t/handling-rest-api-redirects/1825 "2018-09-05T17:07:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmgpeeters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jmgpeeters/32/951_2.png) [@jmgpeeters](https://discuss.konghq.com/u/jmgpeeters)\
**Post date:** [September 5, 2018, 5:07pm UTC](https://discuss.konghq.com/t/handling-rest-api-redirects/1825/1 "2018-09-05T17:07:10Z")

</div>

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,

- [kong.mydomain.com](http://kong.mydomain.com), running Kong on port 8000
- [testapi.mydomain.com](http://testapi.mydomain.com), running our REST API on port 10056,
- [joris.mydomain.com](http://joris.mydomain.com), which is my own machine.

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`:

```auto
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:

```auto
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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![jmgpeeters](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/jmgpeeters/32/951_2.png) [@jmgpeeters](https://discuss.konghq.com/u/jmgpeeters)\
**Post date:** [September 7, 2018, 3:00pm UTC](https://discuss.konghq.com/t/handling-rest-api-redirects/1825/2 "2018-09-07T15:00:45Z")

</div>

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!
