Rich Plugins - Transforming Responses

Hi folks,

We’re trying out tyk gateway (on premise) and we’re impressed by the features and simplicity.

Recently we’re trying to implement a rich plugin for transforming responses (translating Simplified Chinese characters in response to Traditional Chinese characters). After reading through the documents, seems like the only hooks available so far are all about dealing with requests (Pre, Post, PostKeyAuth, CustomAuthCheck) and there’s no way to transform response using a rich plugin.

Have I missed something here? Thanks very much!

Hi Albert, this sounds like an interesting use case. At the moment, you may take control of the request flow and override the return HTTP response code and body (see our Protocol Buffers definitions here and here).

What programming languages do you plan to use?

Best.

Hi matiasb,

Thanks for prompt reply! I will checkout the definitions. We plan to use Python but we’re flexible with it if other languages are more handy.

Hi matiasb,

Any hints on how to get started with “take control of the request flow and override the return HTTP response code and body”? I understand that current protobuf definitions allow me to override response code from a plugin, and probably I need to make the request to origin server in the plugin and transform response thereafter (since there is currently no “post-response” hook).

However I’m totally new to the tyk codebase and not familiar with the request flow in Tyk (i.e. how the request goes through built-in middleware(s), plugins and passed to origin servers), any hints would be highly appreciated!

The response middleware component has not been enabled with rich plugins yet, so you can;t process outbound requests with a plugin.

Instead, you would need to use an inbound post-auth middleware and proxy the request yourself to process the response before it hits the user.

Hey @Martin thanks for the help mentioned above, I have the same use case, may you please elaborate on how to “proxy the request yourself to process the response before it hits the user.”, most examples in the GH repo have to return the miniRequestObject, I couldn’t find a way to do that.

Thanks in advance

Basically - your plugin would need to make the outbound call in code, process the request and then just return the object instead of having Tyk do it - it’s not an ideal scenario because it means you need to basically write a mini proxy.

Has anything changed regarding capabilities to transform responses since March? Currently this seems to be a blocker preventing us from using Tyk.

Example: We have an internal microservice to manage clusters. We call it like this to create a new cluster:

POST /v1/clusters/
Content-type: application/json
{
   "details": "..."
}

The service responds like this:

HTTP/1.1 201 Created
Location: /v1/clusters/new-cluster-id/
Content-type: application/json
{
   "code": "CLUSTER_CREATED",
   "message": "A new cluster with ID 'new-cluster-id' is has been created."
}

Here, the location header contains is the details URI of the new cluster.

We would like to publish this endpoint as POST /v4/clusters/, and the detail URL returned in the Location header should be adapted accordingly, like:

POST /v4/clusters/
Content-type: application/json
{
   "details": "..."
}

Response

HTTP/1.1 201 Created
Location: /v4/clusters/new-cluster-id/
Content-type: application/json
{
   "code": "CLUSTER_CREATED",
   "message": "A new cluster with ID 'new-cluster-id' is has been created."
}

I would expect this to be not an uncommon requirement, so I am a bit puzzled to see that Tyk middleware scripting only affects the request.

Regarding response header modification, I found Fixing response headers that leak upstream server data which seems to allow to adapt the host name and port, but not the path in a Location header.

While the example above only would need adaptation of a header, we also have lots of examples where the body should be modified, too. For example to remove details that shouldn’t be published to certain users.

Also I’m wondering what the section A note on response modifying middleware is about, really. Can you point me to an example?

So basically, I’d be happy for some guidance on how we could solve our case in Tyk Is there any other way than “proxy the request yourself to process the response before it hits the user”? Thanks!

We do not have response processing rich plugins yet I’m afraid, you can only do body transforms using templates on responses.

How about registering custom function that can be used with go template when transforming response ? Is it possible ?