Content/Pay load rewrite like Apache Module mod_substitute

Hi,
We have a requirement that we need to replace the host and url of the response from upstream.
The reason is the upstream response contains its own URL, which should be shown as the endpoint URL.
May I know if it’s possible to achieve this?
Looking at Request Body, I need to have a template, but for my-case, it’s not fix template.

Thanks,
Afri

In this case it will probably require using virtual endpoint. You will make HTTP request on your own, inside Javascript, will dynamically modify response body, and return it back to the user.

Thanks @leon for the suggestion,
May I know if the virtual endpoint can be configured at root “/”. It doesn’t seem to work for me? :frowning:

It can be a bug :frowning:

Can you fill it to github?

It’s my mistake, not a bug :slight_smile:

Now slighly different question,
Can I make a websocket request to upstream/backend service?

What I’m trying to achieve is to have a websocket frontend/endpoint with content rewrite (that I can achieve using virtual-endpoint). However when the response from upstream/backend is not “text”, it will throw exception as TykMakeHttpRequest(JSON.stringify(newRequest)) only supports text response.

Thanks,
Afri

Hi Afrizal,

I’m not sure it makes sense use TykMakeHttpRequest for a ws connection. TykMakeHttpRequest expects a json string as it’s argument, and should be able to deserialise it. If the JSVM is unable to deserialise it, you should see an error in the gateway logs:

JSVM: Failed to deserialise HTTP Request object

You can however create an API within Tyk who’s upstream target is something like: ws://some-socket-server:3333/

You should then be able to get a client to connect with your ws server via the gateway. The gateway should be able to upgrade the connection.

Please ensure "enable_websockets": true is set within your tyk.conf file

https://tyk.io/docs/other-protocols/websockets/

Hi @ahmet,
Thanks for responding. I created a HTTP endpoint (API) with objective to have it as “reverse-proxy”, means it will proxy all the HTTP request (text, image, css). This works so far.
However due to the upstream is rendering URL in absolute format. I’m forced to create API with “slug” the same as the upstream context-root.

In order to allow me to have different slug than my upstream context-root, I tried to to create a virtual endpoint on the root “/” where I do regex replace. The URL is working now, but i face other issues:

  • I can’t forward the header from upstream response to the API, especially for content-type header, whatever I set, it always rendered as “text”.
  • Upstream response that is not text will throw error (See previous post)

Do you think I’m on the right track?