How to configure Krakend so it return http redirect response as-is instead of following the http redirect?

2.8k Views Asked by At

I am currently using Krakend (https://krakend.io) API Gateway to proxy request to my backend service. One of my backend service API response is a redirect response with http 303. The redirect response looks like this below:

HTTP/1.1 303 See Other
content-length: 48
content-type: text/plain; charset=utf-8
date: Thu, 16 Jul 2020 10:25:41 GMT
location: https://www.detik.com/
vary: Accept
x-powered-by: Express
x-envoy-upstream-service-time: 17
server: istio-envoy

The problem is that, instead of returning the http 303 response to client (with location response header) as-is, Krakend is actually following the http redirect and return the response of the redirect Url, which is the html response of https://www.detik.com/.

My current krakend configuration looks like this below :

{
  "version": 2,
  "extra_config": {
    "github_com/devopsfaith/krakend-cors": {
      "allow_origins": [],
      "expose_headers": [
        "Content-Length",
        "Content-Type",
        "Location"
      ],
      "allow_headers": [
        "Content-Type",
        "Origin",
        "X-Requested-With",
        "Accept",
        "Authorization",
        "secret",
        "Host"
      ],
      "max_age": "12h",
      "allow_methods": [
        "GET",
        "POST",
        "PUT"
      ]
    },
    "github_com/devopsfaith/krakend-gologging": {
      "level": "ERROR",
      "prefix": "[GATEWAY]",
      "syslog": false,
      "stdout": true,
      "format": "default"
    },
    "github_com/devopsfaith/krakend-logstash": {
      "enabled": false
    }
  },
  "timeout": "10000ms",
  "cache_ttl": "300s",
  "output_encoding": "json",
  "name": "api-gateway",
  "port": 8080,
  "endpoints": [
    {
      "endpoint": "/ramatestredirect",
      "method": "GET",
      "extra_config": {},
      "output_encoding": "no-op",
      "concurrent_calls": 1,
      "backend": [
        {
          "url_pattern": "/",
          "encoding": "no-op",
          "sd": "static",
          "extra_config": {},
          "method": "GET",
          "host": [
            "http://ramatestredirect.default.svc.cluster.local"
          ],
          "disable_host_sanitize": false
        }
      ]
    }
  ]
}

So how can I make krakend to return original http 303 response unaltered from my backend service to the client ?

3

There are 3 best solutions below

0
On

i faced the same problem while working on google authentication, the api gateway was not redirecting the client to google login website instead fetching the html response of the url and sending it to the client: https://www.krakend.io/docs/enterprise/backends/client-redirect/

the solution is to tell your browser that there is a redirection from server :

return res.status(300).header('Location', yourUrl).send('Redirecting to...');

0
On

If you use Lura Framework (formerly known as Kraken framework), then you may have to disable redirects for your http client.

client := &http.Client{
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}
2
On

I assume that you're calling this endpoint /ramatestredirect

To get backend http status code (as you said it return 303 http status code), you can use this way:

{
  "endpoint": "/ramatestredirect",
  "method": "GET",
  "extra_config": {},
  "output_encoding": "no-op",
  "concurrent_calls": 1,
  "backend": [
    {
      "url_pattern": "/",
      "encoding": "no-op",
      "sd": "static",
      "extra_config": {
        "github.com/devopsfaith/krakend/http": {
          "return_error_details": "authentication"
        }
      },
      "method": "GET",
      "host": [
        "http://ramatestredirect.default.svc.cluster.local"
      ],
      "disable_host_sanitize": false
    }
  ]
}

So, basically with this plugin you can get the original backend http status code

"github.com/devopsfaith/krakend/http": {
          "return_error_details": "authentication"
        }