Kong / Kong/developer.konghq.com

Add example for routing based on model field in POST payload

Open
#2,742 0 comments 0 reactions 0 assignees View on GitHub
internal product:ai-gateway
Dominant language
Ruby
Stars
28
Forks
121
Avg merge
1d 4h
Merged PRs (30d)
313

Description

Sometimes users need to route based on a value from a POST payload. A workaround is to use a global pre-function in the **rewrite** phase to read the relevant content from the payload and add it as a header, then use header-based routing in the [route definition](https://developer.konghq.com/gateway/entities/route/).

Here’s an example that extracts the **AI model** to invoke based on a JSON field in the payload:

```lua
if not kong.request.get_header("X-Model") then
local b = kong.request.get_body("application/json")
if b and b.model then
kong.service.request.set_header("X-Model", b.model)
end
end
````

We must note that users should be careful with this pattern. Parsing/transforming payloads can be time-consuming. While small payloads have minimal cost, a malicious actor could send a very large payload to create memory pressure on Kong.

We should also recommend pairing this approach with the **Request Size Limiting** plugin.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start from the linked route definition documentation and identify the appropriate routing example section. Add the Lua example for extracting the model from a JSON POST payload, explain the rewrite-phase and header-routing pattern, and document the payload-size risk with a recommendation to use the Request Size Limiting plugin.

Written by the indexing model from the issue text.

Assessment

Tech stack
lua
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
58/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.