Using context variables in URL rewrite

I want to use a single value from multiple URL-Parameters in URL Rewrite.

Input URL is like:

http://my.api/endpoint?param1=value1&param2=value2

that should be rewritten as

http://external.api/endpoint/value2

I activated context variables for my API on so I get $tyk_context.request_data filled correctly, but how to access the single value for param2 when request_data is “param1:value1;param2:value2”

I tried the following rewrite templates:

customer-groups/$tyk_context.request_data.value2 → …/endpoint/param1:value1;param2:value2.value2
customer-groups/$tyk_context.request_data[param2] → …/endpoint/param1:value1;param2:value2.[param2]

You could just use a standard URL rewrite?

URL Pattern: endpoint?{params}
Match: (param2=)(\w+)$
Rewrite: endpoint/$2

Nice sandbox example: RegExr: Learn, Build, & Test RegEx

oh, thanks. of cause that will work. Didn´t think about that :slight_smile:

but anyway, would there be a possibility to access a single value from the context variable?

the same question would apply to body transform as I would like to inject a parameter value into the body data

In the URL rewriter - not yet
In the body transform I think you can use dot notation.

yes, in the documentation for body transform it is mentioned, that this is only possible here and not for URL rewriting

@Martin sorry. I have a question with a regex

Assume url: endpoint?{params}
on calling the API i need my url to be like:
http://tyk.com/listen/endpoint?name=“mynae”&id=“myid”

Then i need to rewrite to: http://api/operation?name=$2&id=$3
How will the regex look like

Hello @Corrensa,

If you know that the URL params are always be in this order ?name=“mynae”&id=“myid” you can do the following regex name="(\w+)"&id="(\w+)" the rewrite should then be http://api/operation?name=$1&id=$2

If the URL params are not going to always be in that order then you’ll need a more complex regex to handle it.

Zaid