Request Transformer Plugin

The Request Transformer plugin transforms the request sent by a client on the fly in Kong, before hitting the upstream server. Give it a try and discuss it here!

Request Transformer plugin documentation

Can we customize the plugin to dynamically parse and change the upstream headers while serving?

The use case is when a client hits kong it would send a custom token which needs to parsed and appended to header while serving upstream URL. If possible can someone help how to do that since I am new to kong it would be helpful if someone can point to right document or link to proceed further?

Sure, you can fork the request-transformer and create your own. Here is Plugin Developer Guide:

Here are the sources for the Request Transformer Plugin:
https://github.com/Kong/kong/tree/master/kong/plugins/request-transformer

And here is the test suite:
https://github.com/Kong/kong/tree/master/spec/03-plugins/15-request-transformer

1 Like

Are there any variables available to the request-transformer plugin during execution? For example upstream service name, request path? I had a look at the docs, but I could only see configuration for setting static values.

Request transformer currently only supports static values AFAIK.

Kong Enterprise Edition’s request-transformer-advanced plugin supports something like you describe @Karthik_B

1 Like

https://github.com/pearsontechnology/kong-rewrite-plugin might be what you are looking for. It allows you to basically script everything that the request-transformer and response-transformer do. We open sourced it a while back and use it daily in our environment, so while it may look stagnant it is actively maintained just very stable.

2 Likes

Thanks all, appreciate the feedback. The service/route concept has really helped pushing requests into upstream services more seamlessly.

Does the request-transformer plugin or the kong-rewrite-plugin allow transforming the Host: header of a request?

You should be able to add/update/remove any header with either plugin.

I am trying a sample plugin which uses lua lambda expression like this:

apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
  name: request-transformation
plugin: request-transformer
config:
  replace:
    headers:
    - Authorization:$((function()
          local value = headers.Authorization
          if not value then
            return
          end
          if value:sub(1, 6) == "Basic " then
            return value            -- was already properly formed
          end
          return "Basic " .. value  -- added proper prefix
        end)())

I am getting ‘unexpected error’ when I test it by firing a request from postman.
What Mistake I am making here?