Demystifying Dynamic URLs: How to Build Them with Kong
Tags: kong, dynamic, url, tutorial
Published: 2024-04-03 09:38:56

A dynamic URL is a web address that can change based on certain factors, unlike a static URL that remains constant. It's like a customizable address that adjusts depending on what you're looking for on a website. 

Here's a breakdown of how they work:

•	Components: Dynamic URLs typically include a base URL and a query string. The base URL is the core address of the website, while the query string contains specific instructions or data separated by "?" and "&" symbols.

•	Example: Imagine an online store. A static URL for a product might be https://www.example.com/shoes/red-sneakers. This works well for a single product. 

But with dynamic URLs, things get more flexible. The URL could look like https://www.example.com/products?color=red&size=10. Here, "products" is the base URL, and the query string specifies "color=red" and "size=10".

Static vs. Dynamic URLs: Choosing the Right Path for Your Website

In the world of web development, URLs (Uniform Resource Locators) play a crucial role in directing users to specific content on your website. But not all URLs are created equal. 

We have two main categories: static URLs and dynamic URLs. Understanding the differences between them is essential for building an efficient and user-friendly website.

Static URLs: The Simpler Sibling

Imagine a static URL as a clear street address. It points directly to a specific location on your website, like a particular page or file. These URLs are typically easy to remember and manage.

      Benefits of Static URLs:

•	SEO Friendly: Search engines love static URLs because they're easy to crawl and understand. This can boost your website's ranking in search results.

•	Simpler Management: Static URLs are straightforward to manage and cache, making them ideal for smaller websites with relatively fixed content.

        Drawbacks of Static URLs:

•	Limited Flexibility: As your website grows, static URLs can become cumbersome. Imagine adding hundreds of products to an online store – each product would require a unique static URL, making maintenance a challenge.

•	Content Updates: Updating information across multiple static pages with the same content can be time-consuming.

Dynamic URLs: Adapting to User Needs

Dynamic URLs, on the other hand, are more like interactive maps. They can change based on user input, database queries, or server-side logic. This allows for a more dynamic and adaptable website experience.

Advantages of Dynamic URLs:

•	Flexibility is Key: Dynamic URLs excel at handling vast amounts of content with a single route. This is perfect for websites with large databases or frequently changing content.

•	Personalized Experiences: By responding to user input, dynamic URLs can create a more personalized experience for each visitor. Imagine a news website displaying news relevant to your location or interests.

•	Streamlined Management: Adding or updating content becomes easier with dynamic URLs. You don't need to create a new URL for each change – the dynamic system takes care of it.
Challenges of Dynamic URLs:

•	Complexity: Setting up dynamic URLs can involve more configuration and management compared to static ones.

•	SEO Considerations: While search engines have become adept at handling dynamic URLs, some SEO best practices need to be followed to ensure proper indexing.

So, Which URL Should You Choose?

The best choice depends on your website's specific needs:

•	Static URLs: Ideal for simple websites with fixed content or where SEO is a top priority.

•	Dynamic URLs: Perfect for websites with extensive content databases, user-generated content, or where personalization is important.

      Demystifying Dynamic Routing in Kong: A User-Centric Approach

At Kong, we frequently hear from customers and potential users about mastering dynamic routing based on URLs and headers. This blog post dives into various dynamic routing scenarios and explores how Kong empowers you to tackle them effectively.

The Essence of API Gateways:

An API gateway sits like a central hub, intelligently directing incoming requests to the appropriate backend service. In Kong, we represent these backend services as "Services”.

Services: The Backbone of Routing:

A Service object serves as the blueprint for an upstream API or service. Its core attribute is the URL, which defines where Kong should forward the traffic. This URL can be a simple string or a more granular configuration specifying protocol, host, port, and path components.

The Power of Dynamic Routing:

The default behavior is fantastic, but what if you need more flexibility? Dynamic routing in Kong allows you to go beyond static URLs and create routes that adapt based on specific elements within the URL and request headers. This unlocks a world of possibilities for managing complex API interactions.

       Next Steps:
In the following sections of this blog post, we'll delve into specific use cases for dynamic routing and showcase how Kong empowers you to implement them to create a robust and adaptable API layer.

Hands-on with Kong Manager: Configuring Dynamic Routing

Now, let's put theory into practice! We'll walk you through setting up dynamic routing in Kong Manager using an example.
Imagine we have an API named "Dynamic_url" that acts as a middleman, forwarding requests to an upstream service located at https://php-noise.com/noise.php


Logging In and Setting Up:
1.	Access Kong Manager: Head to your Kong Manager dashboard and log in.

2.	Choose Your Workspace: Here we need to specify the workspace. I'll be working in the default workspace.

3.	Navigate to Services: Click on the "Services" tab to manage your backend services.

4.	Adding a New Service: Within the "Services" section, locate the "Add a Service" button and click on it.

Instead of defining a specific path segment in the service itself, I'll simply use a forward slash ("/") for the path. This way, I can achieve more flexibility by specifying the actual upstream path later using the Request Transformer plugin.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mfgolpa8t0kw6bckbo3e.png)

Continuing Our Kong Manager Setup: Creating the Route
Now that we've created the "Dynamic_url" service representing our backend API, let's define a route within this service to handle dynamic routing. This route will tell Kong how to handle requests that include the path "/common".


Connecting the Dots with a Route:
1.	Accessing the Dynamic_url Service: Within Kong Manager, find the "Dynamic_url" service we just created. Click on it to access its details.

2.	Adding a Route: Look for a button or option labeled "Add Route" or something similar. Click on it to initiate the route creation process.

3.	Naming the Route: Provide a descriptive name for your route. In this case, let's call it "Dynamic-Route" to reflect its purpose for testing dynamic routing.

4.	Defining the Path: Locate the field labeled "Path(s)". Here, we'll specify the path segment that will trigger this route. Enter /common in this field.

5.	Specifying the HTTP Method: In addition to defining the path, Kong routes also allow you to specify the HTTP method that the route handles. Since we're focusing on fetching data from the "Dynamic_url" service, we'll use the GET method.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/krc72zgb85vrqiugwx6t.png)

Let's move forward with enhancing our Kong route by incorporating three plugins: Basic Authentication, ACL, and Request Transformer. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q773a8097zr4gdtzkqon.png)

These plugins offer powerful functionalities to secure and manage your API traffic. Here's a breakdown of why each plugin is valuable:

1. Basic Authentication Plugin:
Purpose: This plugin adds a layer of security by requiring users to provide valid credentials (username and password) before accessing the route. This helps restrict access to authorized users only.

Why Use It: Let say our "Dynamic_url" service handles sensitive data, implementing basic authentication ensures only authorized users can interact with it. This helps prevent unauthorized access attempts.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pavwv9ioewng6s59uuni.png)

2. ACL (Access Control List) Plugin:

Purpose: The ACL plugin provides fine-grained control over who can access the route. You can define rules based on factors like consumer credentials, IP addresses, or custom attributes.

Why Use It: The ACL plugin goes beyond basic authentication by allowing you to create more granular access policies. You can define specific permissions for different users or groups, ensuring only those who meet the criteria can access the route.
Let's refine the explanation of the ACL plugin configuration in our Kong route to specify the allowed consumer group.

Focusing on the ACL Plugin:

Within the ACL plugin configuration for our route, we’ll be defining an access control rule that specifically allows access to consumers belonging to the group named "consumer1".

Why Consumer Group "consumer1"?
Assuming you have a mechanism for managing consumer groups in Kong, this configuration restricts access to the route for the "Dynamic_url" service to only those consumers who are explicitly assigned to the "consumer1" group. This provides a controlled access layer for your API.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jersea5r2ozhcl1ogpag.png)

3. Request Transformer Plugin:
Purpose: This plugin allows you to modify the request before it's forwarded to the upstream service. You can manipulate headers, parameters, or even the request body.

Why Use It: The Request Transformer plugin offers flexibility in how you prepare requests before sending them upstream. You can use it to:

•	Add or remove headers for specific purposes.

•	Modify request parameters based on routing logic.

•	Transform the request body to match the format expected by the upstream service.

Now, to dynamically determine the path forwarded to the upstream service, we'll configure the Request Transformer plugin to modify the request URI. We'll specify the upstream path as "/noise.php" within the plugin.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bbwi8tgbnsj0794430vk.png)

By strategically using these plugins in your Kong route, we can create a secure and well-managed API environment for your "Dynamic_url" service.

With a well-configured route and plugins in place, we need to define a consumer for this route. Think of a consumer as an entity that interacts with our Kong API. It could represent:

•	An external application: This could be a mobile app, another web service, or any external system that needs to access your API through Kong.

•	An internal application: Within your organization, different internal applications might require access to your API endpoints. Consumers allow you to manage access for each of these entities.

Consumer and Security Configuration
•	Consumer "consumer1": We've created a consumer named "consumer1" in Kong. This consumer will represent the entity authorized to access the route.

•	ACL Plugin and Consumer Allowance: Within the ACL plugin configuration for the route, we’ve established a rule that specifically allows access for the "consumer1" consumer. This ensures only "consumer1" can interact with this route based on our chosen access control policy.

•	Basic Authentication User: An additional layer of security is implemented through basic authentication. I've created at least one basic authentication user (username and password combination). When the "consumer1" consumer attempts to access the route, it will need to provide valid credentials associated with this user to gain access.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/318j9exm4jqhwz01khdx.png)

Now that we've configured the route and security in Kong, it's time to test its functionality using Insomnia, a popular HTTP client. 

We'll focus on testing without enabling dynamic path manipulation for now.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xpw9qyowdn09n1x29941.png)

Test Results: Success!
That's fantastic! Our testing in Insomnia has yielded positive results:

•	200 OK Response: When we send a request to the route through Kong using Insomnia, we receive a 200 OK status code. This indicates successful communication and suggests Kong is functioning as expected, forwarding the request to the upstream service.

•	Consistent Response from Upstream: Additionally, if we directly access the upstream URL (php-noise.com/noise.php) outside of Kong, we receive the same 200 OK response. This confirms that the upstream service itself is also operational and returning the expected response.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1132l6lyozewzp74biss.png)

Testing the Local API (Static Parameters):
Now that we understand the functionality of the URL, let's move forward with testing it in Insomnia (or your preferred HTTP client) to verify its behavior. In this initial test, we'll focus on using the URL exactly as provided, without enabling dynamic routing features in Kong.

Here's what we're aiming to achieve:

•	Sending a Request: Make a request to the complete URL: http://localhost:8000/common/noise.php?r=0&g=55&b=200&tiles=50&tileSize=7&borderWidth=0&mode=brightness&json 

•	Verifying Response: Observe the response from the script. Ideally, you should receive a 200 OK status code indicating successful communication. The response body should contain the generated noise data in JSON format, based on the parameters provided:

•	Color: Red (r=0), Green (g=55), Blue (b=200)
•	Tile layout: 50 tiles (tiles=50)
•	Tile size: 7 pixels each (tileSize=7)
•	No border (borderWidth=0)
•	Brightness mode (mode=brightness)

By testing with the complete URL and its pre-defined parameters, we're establishing a baseline for the script's functionality without introducing dynamic elements through Kong. This will help us isolate any issues later when we incorporate dynamic routing.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bke2i21lb37crxn9tml1.png)

While the current response isn't what we ultimately aim for, it's understandable as we haven't implemented dynamic routing in Kong. Let's move on to that step.

Enabling Dynamic Routing in Kong: Capturing the Path with (?<path>\S*)

Now that we've established a baseline for the local script's functionality, let's move forward and enable dynamic routing in Kong. This will allow us to capture a dynamic part of the URL path and use it to construct the upstream path for requests.

Introducing the (?<path>\S) Regular Expression:

The first step involves adding a regular expression (?<path>\S*) to our Kong route configuration. Let's break down this expression and understand its purpose:

•	(?<path>: This part defines a capturing group named "path". Captured values within this group will be accessible later using the name "path".

•	\S*: This matches any sequence of non-whitespace characters (zero or more times). This captures the dynamic portion of the URL path that follows the static /common segment.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hf13r0myh2mp1mo7l94n.png)

Incorporating Captured Path into Upstream Path: Using $(uri_captures['path']) in Request Transformer
Now that we've captured the dynamic path segment using the regular expression in the route, let's see how to utilize it to construct the upstream path dynamically. This is where the Request Transformer plugin and the expression $(uri_captures['path']) come into play.

Understanding $(uri_captures['path']):

•	Accessing Captured Values: This expression acts as a placeholder within the Request Transformer plugin to access values captured by named groups in regular expressions within your routes.

•	Referencing the "path" Group: Specifically, $(uri_captures['path']) references the value captured by the "path" group in your route's regular expression.
Dynamic Path Construction:

Here's how this works in action:

•	Request Arrival: A request with a path like /common/data456 arrives at Kong.

•	Capturing Path Value: The route captures data456 in the "path" group.

•	Plugin Transformation: The Request Transformer plugin takes over. It replaces the static path in the request with a new path constructed using $(uri_captures['path']).

•	Dynamic Path Formation: The plugin replaces $(uri_captures['path']) with the actual captured value (data456), resulting in a new path, likely /noise.php in our case.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p6dlghmpxj9t8dqqubdw.png)

Ready to Test with Dynamic Routing!

With dynamic routing now enabled in Kong, we've reached an exciting stage. The regular expression in your route captures dynamic path segments, and the Request Transformer plugin utilizes $(uri_captures['path']) to construct the upstream path based on the captured value.

Time for Insomnia Testing (Round 2):

Let's revisit Insomnia and test the same API again:

http://localhost:8000/common/noise.php?r=0&g=55&b=200&tiles=50&tileSize=7&borderWidth=0&mode=brightness&json


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/necaiw4ajnk8s19jr79q.png)

Success! Dynamic Routing Delivers!

That's fantastic news! By testing the API again in Insomnia we're now receiving the expected response. 

This successful test demonstrates that we've effectively implemented dynamic routing in Kong. Now, our API can handle requests with various path segments after "/common" and route them appropriately to our noise.php script for processing.

Insomnia Test Round 3: Exploring Additional Parameters

Now, let's move on to round 3 of testing in Insomnia to explore how the API handles additional parameters:

New API:
http://localhost:8000/common/noise.php?hex=${hex}&json&base64


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ekphhvo5hl4byh4imu0c.png)

Excellent! The successful response from the API confirms that our dynamic routing configuration in Kong is functioning flawlessly.

Conclusion: Mastering Dynamic Routing with Kong

In this post, we've explored how to leverage Kong's dynamic routing capabilities to achieve flexible API routing. We started by establishing a baseline for a local script (noise.php) that generates visual noise data. Then, we configured Kong to dynamically handle requests with varying path segments:

•	Capturing Dynamic Paths: We utilized a regular expression in the route definition to capture dynamic portions of the URL path after a static prefix.

•	Request Transformation: The Request Transformer plugin played a crucial role. It dynamically modified the upstream path within the request based on the captured value using $(uri_captures['path']). This allowed Kong to route requests to the appropriate upstream service (noise.php) regardless of the specific path segment used.

Benefits of Dynamic Routing:

•	Flexibility: Dynamic routing allows you to handle a wider range of URL structures within your API without requiring separate routes for each variation.

•	Maintainability: By using a single route with dynamic path capturing, you simplify Kong configuration and reduce the need for managing numerous static routes.

•	Control Over Upstreams: The captured path segment can be used not only for routing but also for manipulating other aspects of the request before forwarding it to the upstream service.