Blog
  • AI Gateway
  • AI Security
  • AIOps
  • API Security
  • API Gateway
|
    • API Management
    • API Development
    • API Design
    • Automation
    • Service Mesh
    • Insomnia
    • View All Blogs
  1. Home
  2. Blog
  3. Engineering
  4. How to Dynamically Route Requests With Kong Enterprise
Engineering
July 31, 2020
4 min read

How to Dynamically Route Requests With Kong Enterprise

Mos Amokhtari

Having worked with many customers and prospects at Kong, one of the main requirements we often hear is how to handle dynamic routing based on the URL and headers. In this blog post, I will cover different use cases we come across for dynamic routing and how Kong can address them.

The default behavior of an API gateway is to route the incoming request to the appropriate upstream service.

If you are new to Kong, a Service object represents the upstream API or Service. The main attribute of a Service is the URL (where Kong should proxy traffic to), which can be set as a single string URL or by specifying the protocol, host, port and path properties.

Steps to Route Kong Enterprise Requests

To make requests to services via Kong, they first need to be exposed by routes. A single service can have many routes to allow different policies to be applied to different clients/consumers accessing the same Service. After configuring services and routes, clients make requests to routes, and the requests will be proxied to services.

One common use case for dynamic routing is to rewrite the requested URL to a different URL for the upstream. This is helpful in instances where the structure of the service's URL has changed, but we don't want to change or impact how the client calls the service. For this use case, Kong can make the change transparent to the client by enabling URL rewriting. My colleague, Peter Kim, wrote a great blog post on this subject that I invite you to read.

Let’s review another use case. Based on a specific header, route the request to a different upstream. This scenario is useful when a new version of an API is available.

Let's walk through an example using Kong Manager to set up the scenario. Note Kong can also be managed through the Admin API as well as declaratively.

Say our API will be represented by a service called httpbin, which forwards requests to an upstream with URL httpbin.org.

Launch Kong Manager, click on the Workspace you want to work with, select Services and click Add a Service.

Create myfirstRoute

Let's now create a route called "myfirstRoute" to access the service through Kong when a path of /requestByheader is provided. Meaning, when a request to Kong is made with path /requestByheader, Kong will forward the request to the httpbin service.

In the httpbin service, click Add route, name the route "myfirstRoute" and under Path(s), enter /reequestByheader.

Let's confirm the service and route are working correctly by using Kong Studio to send a request to Kong and the route we created. As expected, the request is forwarded to the upstream httpbin.org.

Enable Dynamic Routing

To enable the dynamic routing use case based on header values, let’s enable the route-by-header plugin on this route. For those new to Kong, a plugin allows Kong to apply policy to request and responses. For this scenario, let's configure the plugin to route to a different upstream, httpbin2, if a header with version "v2" is part of the request.

Let's use the Admin API to configure the plugin on the route we created. The following command configures the route-by-header plug-in on the myfirstRoute to route to the upstream httpbin2 when a header version:v2 is part of the request.

curl -i -X POST http://<kong_host>:8001/routes/myfirstRoute/plugins -H ‘Content-Type: application/json’ –data ‘{"name": "route-by-header", "config": {"rules":[{"condition": {"version":"v2"}, "upstream_name": "httpbin2"}]}}’

Sending the same request as before using Kong Studio, but this time with a header of version v2, sends the request to httpbin2 instead of httpbin.org.

Many customers use this capability to enable A/B testing or to slowly migrate users from one version to another.

Target Upstream Requests

Another use case is that you may want to provide the target upstream in your request (in the URI, query parameters or in a header). One possible use case is, for example, the same service is hosted on different instances but specific to an end customer. One of our telecommunications customers providing network managed services uses this scenario to give access to the right EMS (Element Management System) to their end customers. The service provided is the same but on a different EMS. The route-transformer-advanced plugin is the most appropriate to do so, as it allows it to transform the routing on the fly in Kong, changing the upstream server, port or path to hit.

Case A: Using a Header

On the previous same route (myfirstRoute), instead of enabling the route-by-header plugin, we enable the plugins route-transformer-advanced.

For now on, for this path (/requestByheader), Kong expects a header called "mytarget" with the target host value.

Case B: Using a Part of the URI (Capture URI String)

First, we need to modify the path in the route. Paths accept regex (as explained in more details here) to allow capturing groups.

Let’s create another route with a path "/requestByUri/(?<target>\w+)"

Apply the route-transformer-advanced plugins using the capture URI method.

And voilà …

One comment here: Some may object that the upstream target may be unknown from the client/consumer side and therefore this won’t be of any use. In that particular case, I recommend that you read my next post on the usage of the serverless function plugin.

Developer agility meets compliance and security. Discover how Kong can help you become an API-first company.

Get a DemoStart for Free
Kong Gateway EnterpriseKong GatewayAPI Gateway

More on this topic

Videos

Kong Builders Nov 16- Introducing Kong Gateway Operator

Videos

Kong Builders - July 22 - Running Kong Gateway on Red Hat OpenShift

See Kong in action

Accelerate deployments, reduce vulnerabilities, and gain real-time visibility. 

Get a Demo
Topics
Kong Gateway EnterpriseKong GatewayAPI Gateway
Share on Social
Mos Amokhtari

Recommended posts

Kong Gateway 3.9: Extended AI Support and Enhanced Security

Kong Logo
Product ReleasesDecember 20, 2024

Today we're excited to announce Kong Gateway 3.9!  Since unveiling Kong Gateway 3.8 at API Summit 2024 just a few months ago, we’ve been busy making important updates and improvements to Kong Gateway. This release introduces new functionality arou

Alex Drag

What's New in Kong Gateway 3.7?

Kong Logo
Product ReleasesMay 29, 2024

We're thrilled to announce the general availability of Kong Gateway 3.7 and Kong Gateway Enterprise 3.7. Along with enhancements and new features for both OSS and enterprise users, this version comes with the general availability of our edge AI Gate

Veena Rajarathna

Kong Konnect EKS Marketplace Add-on for Kong Gateway Data Planes

Kong Logo
EngineeringDecember 7, 2023

Today, we’re excited to release the Kong Konnect EKS Marketplace add-on as a means to deploy your Kong Gateway dataplanes in AWS. The add-ons are a step forward in providing fully managed Kubernetes clusters. It is here to simplify the post-procurem

Danny Freese

Gateway API: From Early Years to GA

Kong Logo
EngineeringNovember 7, 2023

In the Kubernetes world, the Ingress API has been the longstanding staple for getting access to your Services from outside your cluster network. Ingress has served us well over the years and can be found present in several dozen different implementa

Shane Utt

Governing GraphQL APIs with Kong Gateway

Kong Logo
EngineeringOctober 20, 2023

Modern software design relies heavily on distributed systems architecture, requiring all APIs to be robust and secure. GraphQL is no exception and is commonly served over HTTP, subjecting it to the same management concerns as any REST-based API. In

Rick Spurgeon

Using Kong Gateway to Adapt SOAP Services to the JSON World

Kong Logo
EngineeringSeptember 6, 2023

While JSON-based APIs are ubiquitous in the API-centric world of today, many industries adapted internet-based protocols for automated information exchange way before REST and JSON became popular. One attempt to establish a standardized protocol sui

Hans Hübner

Securing your Services and Applications with Styra Declarative Authorization Service (DAS) & Kong Gateway Enterprise

Kong Logo
EngineeringSeptember 26, 2022

Jeff Broberg, William Seaton and Peter Sullivan from Styra also contributed to this post API Gateway Authentication (AuthN) and Authorization (AuthZ) are important ways to control the data that is allowed to be transmitted using your APIs. Basically

Claudio Acquaviva

Ready to see Kong in action?

Get a personalized walkthrough of Kong's platform tailored to your architecture, use cases, and scale requirements.

Get a Demo
Powering the API world

Increase developer productivity, security, and performance at scale with the unified platform for API management, AI gateways, service mesh, and ingress controller.

Sign up for Kong newsletter

    • Platform
    • Kong Konnect
    • Kong Gateway
    • Kong AI Gateway
    • Kong Insomnia
    • Developer Portal
    • Gateway Manager
    • Cloud Gateway
    • Get a Demo
    • Explore More
    • Open Banking API Solutions
    • API Governance Solutions
    • Istio API Gateway Integration
    • Kubernetes API Management
    • API Gateway: Build vs Buy
    • Kong vs Postman
    • Kong vs MuleSoft
    • Kong vs Apigee
    • Documentation
    • Kong Konnect Docs
    • Kong Gateway Docs
    • Kong Mesh Docs
    • Kong AI Gateway
    • Kong Insomnia Docs
    • Kong Plugin Hub
    • Open Source
    • Kong Gateway
    • Kuma
    • Insomnia
    • Kong Community
    • Company
    • About Kong
    • Customers
    • Careers
    • Press
    • Events
    • Contact
    • Pricing
  • Terms
  • Privacy
  • Trust and Compliance
  • © Kong Inc. 2025