Conditional backend

44 views
Skip to first unread message

Evgenii Volokhonskii

unread,
Jan 27, 2025, 3:05:53 PMJan 27
to comm...@krakend.io
Hello,

I'm struggling with separating requests between two backends and couldn't find any solution. 
I have a KrakenD endpoint that receives SOAP messages. I need to call different backend services depending on the message content. For example, if the message body contains <operation namespace="security">, the Security service needs to be called. Otherwise, it should call the Product service. Could you please help me to figure out if this functionality can be implemented at all and if so, what should be the approach?

Jorge Tarrero

unread,
Jan 28, 2025, 3:43:58 AMJan 28
to KrakenD Community, Evgenii Volokhonskii
Hi Evgenii,

A simple way would be to use Lua scripting,  with that we can intercept the request before hitting the backends, evaluate the body and select where you want Krakend to go next. This is a working example configuration file:

{
"version": 3,
"echo_endpoint": true,
"endpoints": [
{
"endpoint": "/",
"method": "POST",
"backend": [
{
"url_pattern": "/__internal_parse_body",
"method": "POST",
"host": ["http://localhost:8080"],
"extra_config": {
"modifier/lua-backend": {
"allow_open_libs": true,
"sources": [
"/path/to/conditional-backend.lua"
],
"pre": "switch_url()"
}
}
}
]
}
]
}

And the Lua script:
function switch_url()
local req = request.load()
local body = req:body()
if string.find(body, '<operation namespace=\"security\">') then
else
end
end

Cheers,
Reply all
Reply to author
Forward
0 new messages