-
I want to add a path parameter, like: local function public_api(conf, ctx) -- not working
-- Extract the path parameter (e.g., `:route_id` from the URI)
local route_id = ctx.var.route_id
return 200, {
msg = "public_api",
route_id = route_id
}
end
-- module interface for export public api
function _M.api()
return {
{
methods = {"GET"},
uri = "/apisix/plugin/demo/public_api/:route_id", -- not working
handler = public_api,
}
}
end |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Yes, you can add a URL (path) parameter to a plugin API endpoint using the So, update your handler like this: local function public_api(conf, ctx)
local route_id = ctx.curr_req_matched and ctx.curr_req_matched.route_id
return 200, {
msg = "public_api",
route_id = route_id
}
end This approach matches how APISIX exposes path variables in the context for parameterized routes, including plugin APIs, as described in the APISIX documentation. If To reply, just mention @dosu. How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other |
Beta Was this translation helpful? Give feedback.
-
Hi, pls check the doc https://apisix.apache.org/docs/apisix/router-radixtree/. and apisix/apisix/utils/router.lua Line 24 in e90570a |
Beta Was this translation helpful? Give feedback.
-
Thanks @SkyeYoung I solved it: {
methods = {"GET"},
uri = string.format("%s/example/*", prefix),
handler = function(ctx)
local path = ctx.var.uri:match("/example/(.*)$") -- extract the path parameter
return 200, {message = "OK", path = path}
end,
} |
Beta Was this translation helpful? Give feedback.
Thanks @SkyeYoung I solved it: