Injecting url params

Hi Martin,

I need to inject a parameter in the url of my api for a specific path. The parameter value would be hardcoded i.e. always having same value.
What plugin would you recommend for doing this (url rewrite or virtual endpoint)?

Would you provide a simple example?


I would suggest a URL rewrite, should do the trick - the plugin is pretty self explanatory, just remember to ensure you matched path is correct.

I’m having difficulty in the regex expressions to do the trick. The main issue is that the parameter to be injected maybe injected solely or added to other parameters that were already passed in the request url.

Then you might be out of luck :slight_smile: we’ve been mulling over adding this feature, but it’s not done yet.

Hi Martin,

Finally I have figured out a regex that would do the trick. I have tried it in a regex tool named Expresso. However, when I applied it to the API, it didn’t work as expected.

I have no idea about the engine executing regex.

I am applying the url rewrite as follows:

-Path: events
-Match Pattern: (?(^events(/)?(?)?.jurisdiction=.$)(events(/)?(?)?(.)(?:[?]?jurisdiction=([^&])&?)(.))|(events(/)?(?)?(.)))
-Rewrite To: EventsRealtimeNoGeoOttawa/events/?$6$8$12

This is supposed to run as follows for the following example path:

-Requested path: events?format=xml&jurisdiction=a
-Rewritten path should become: EventsRealtimeNoGeoOttawa/events/?
-Rewritten path returned from tyk: EventsRealtimeNoGeoOttawa/events/?

Please I need your advice.

Hi Ahmad,

When I try that Regex I get an error that it isn’t valid Perl syntax, so I’m not sure you’ve given me a valid regex there - for example, the forward-slashes need escaping and your dot-matches were not matching more than one character where they were meant to break up things like the format param block.

I’ve broken it down to something simpler, and this works:

  • Path: events
  • Match pattern: (events(\/)?(\?)?)(format=)([a-zA-Z]+)((&)?)(jurisdiction=)(.*$)
  • Rewrite to: EventsRealtimeNoGeoOttawa/events/?$4$5

The tool I use to create compatible regexes is called Regexr it’s excellent for creating and identifying your match groups that are compatible with Tyk Gateway.

I can see that your regex is attempting to capture many permutations of the inbound path, so you may want to play with that a little.

I hope that helps :slightly_smiling:


Hi Martin,

Thank you for the great support. I really appreciate it.

What I’m trying to achieve is to inject a query parameter( and preserve all other query parameters passed in the request. Moreover, I’m checking for the jurisdiction parameter if it is already in the request, then I remove it to prevent having this param duplicated.

I used your regex tool to modify the expressions and simplified them. Now these expressions work in the tool:
Match Pattern: ((events(/)?(?)?(.)(?:[?]?jurisdiction=([^&])&?)(.))|(events(/)?(?)?(.)))
Replace Patter: EventsRealtimeNoGeoOttawa/events/?$5$7$11

Unfortunately, it is still not working. When I place a request I’m getting perl error:
“error”: "error parsing regexp: invalid or unsupported Perl syntax: (?="

There is no syntax “(?=” inside my regex. I’m not sure if the modification has not taken place.

Hi Ahmad,

I still don’t think that your Regex is correct, when I apply it to your test URL it matches the wrong part of the string, it should match the entire thing in order trap groups:

The error you are getting is probably a compilation error from the OLD URL rewrite (it can take a few seconds to propagate to our cloud / hybrid (tail the logs to be sure that the new API def has been picked up, because that was the same error I got with your Old regex).

You new one seems to partially work, a least in my debug environment:

> [Mar  5 10:14:52]  INFO URL Re-written from: /closed/events?format=xml&jurisdiction=a
> [Mar  5 10:14:52]  INFO URL Re-written to: EventsRealtimeNoGeoOttawa/events/?

I’m not a regex expert, so I can’t really guide on how to write one :-/ But I can help you validate yours.

1 Like

Hi Martin,

That is strange since the regex is replacing as I need in the tool regxr. Kindly see attached how the replace result is exactly what I want. It added the new query param, replaced the duplicate one and preserved other query params:

Moreover, the error returned is the same. I’m not sure why the update has not taken place yet.

Hi Ahmad,

How very odd.

I’ve created a little Go Playground app that is basically the core code of the URL rewriter so you can debug your Regexes more easily with the real thing, this code is pulled directly out of the middleware, so will mimic what to expect in Tyk Cloud since they run the same core code.

To use it, just edit the variables in place and click the “Run” button, that will execute the code and return output underneath the editor. This should help you debug your injector somewhat.

You may have actually identified a bug as well, as I think we only support 9 match groups. I’ve pushed a fix to our cloud and to our hybrid container, so you may need to restart your hybrid agent to get the benefit.

I really really appreciate your help.

What you mentioned regarding the support for max of 9 matching groups was the issue. I have reduced the number of matching groups and now it works like a charm.

Thanks again,

Thanks Martin the Regexr tool which you shared really helped me to overcome my issue :slightly_smiling_face: