Using expressions in target URLs allows you to dynamically route requests based on path parameters, headers, or other request attributes.
In this example we will route three XML messages based on their content to three different destinations.
To run the example execute the following steps:
-
Go to the folder
examples/routing-traffic/dynamic-routing -
Start Membrane:
membrane.sh or
membrane.cmd
- Access the Fruit Shop API through the alternate URL:
curl localhost:2000/market/products- Or try accessing different API endpoints:
curl localhost:2000/market/vendors
curl localhost:2000/market/customers
curl localhost:2000/market/ordersEach request will be dynamically routed to the corresponding endpoint at https://api.predic8.de/shop/v2/[endpoint] based on the path parameter you provide.
<api port="2000" name="Router">
<path>/market/{page}</path>
<target url="https://api.predic8.de/shop/v2/${pathParams.page}" />
</api>This configuration captures the page path parameter and dynamically inserts it into the target URL using the expression ${pathParams.page}.