Rewrite upstream URL in plugin (Python)

Hi All,

Is it possible to rewrite an upstream url within a Python plugin? We have a use case where we need to rewrite the url based on some business logic so cannot use the rewrite rules that can be added along with the API.

I’ve tried setting a new string in request.object.url but that does not have any effect.

It looks like it is possible with JS middlewares but we need to use Python as we would like to use a Redis library to pull a HSET.

Hoping it is possible, or more than willing to submit a PR if someone, maybe @matiasb? Can point me in the right direction. :grin:

Cheers
Tom

Tried the following:

# middleware.py
from tyk.decorators import Hook

@Hook
def PostMiddleware(request, session, metadata, spec):
  request.object.url = '/hello/world'

  return request, session, metadata
# manifest.json
 {
  "file_list": [
    "middleware.py"
  ],
  "custom_middleware": {
    "post": [
      {
        "name": "PostMiddleware",
        "require_session": false
      }
    ],
    "driver": "python"
  },
  "checksum": "",
  "signature": ""
}

I just raised a PR with the changes needed to allow urls to be rewritten within coprocess middlewares. Hopefully this is something we can get merged.

As I mentioned in the PR, happy to discuss and make any changes if there is a better approach.

Awesome! Thanks :slight_smile: we’ll get itnjntk review!