This is an automated email from the ASF dual-hosted git repository.
baoyuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new b2d14df37 fix: Adding request-id header in case of empty header value
in request (#12837)
b2d14df37 is described below
commit b2d14df3769c0f8330481d41417bdd597aea7b49
Author: Shubham Mishra <[email protected]>
AuthorDate: Wed Jan 28 09:33:52 2026 +0530
fix: Adding request-id header in case of empty header value in request
(#12837)
Co-authored-by: Baoyuan <[email protected]>
---
apisix/plugins/request-id.lua | 6 ++++--
docs/en/latest/plugins/request-id.md | 14 +++++++++++++-
t/plugin/request-id.t | 28 ++++++++++++++++++++++++++++
3 files changed, 45 insertions(+), 3 deletions(-)
diff --git a/apisix/plugins/request-id.lua b/apisix/plugins/request-id.lua
index 71e354f4f..5fc9cf731 100644
--- a/apisix/plugins/request-id.lua
+++ b/apisix/plugins/request-id.lua
@@ -102,7 +102,8 @@ end
function _M.rewrite(conf, ctx)
local headers = ngx.req.get_headers()
local uuid_val
- if not headers[conf.header_name] then
+ local header_req_id = headers[conf.header_name]
+ if not header_req_id or header_req_id == "" then
uuid_val = get_request_id(conf)
core.request.set_header(ctx, conf.header_name, uuid_val)
else
@@ -123,7 +124,8 @@ function _M.header_filter(conf, ctx)
end
local headers = ngx.resp.get_headers()
- if not headers[conf.header_name] then
+ local header_req_id = headers[conf.header_name]
+ if not header_req_id or header_req_id == "" then
core.response.set_header(conf.header_name, ctx["request-id-" ..
conf.header_name])
end
end
diff --git a/docs/en/latest/plugins/request-id.md
b/docs/en/latest/plugins/request-id.md
index 3d998ac21..7edea624f 100644
--- a/docs/en/latest/plugins/request-id.md
+++ b/docs/en/latest/plugins/request-id.md
@@ -32,7 +32,7 @@ description: The request-id Plugin adds a unique ID to each
request proxied thro
## Description
-The `request-id` Plugin adds a unique ID to each request proxied through
APISIX, which can be used to track API requests. If a request carries an ID in
the header corresponding to `header_name`, the Plugin will use the header value
as the unique ID and will not overwrite with the automatically generated ID.
+The `request-id` Plugin adds a unique ID to each request proxied through
APISIX, which can be used to track API requests. If a request carries an ID in
the header and is not empty ("") corresponding to `header_name`, the Plugin
will use the header value as the unique ID and will not overwrite with the
automatically generated ID.
## Attributes
@@ -99,6 +99,18 @@ You should receive an `HTTP/1.1 200 OK` response and see the
response includes t
X-Request-Id: b9b2c0d4-d058-46fa-bafc-dd91a0ccf441
```
+Send a request to the Route with a empty request ID in the header:
+
+```shell
+curl -i "http://127.0.0.1:9080/anything" -H 'X-Request-Id;'
+```
+
+You should receive an `HTTP/1.1 200 OK` response and see the response includes
the `X-Request-Id` header with a generated ID:
+
+```text
+X-Request-Id: b9b2c0d4-d058-46fa-bafc-dd91a0ccf441
+```
+
Send a request to the Route with a custom request ID in the header:
```shell
diff --git a/t/plugin/request-id.t b/t/plugin/request-id.t
index 39e9f9430..5ee7753cf 100644
--- a/t/plugin/request-id.t
+++ b/t/plugin/request-id.t
@@ -505,3 +505,31 @@ X-Request-ID: 123
--- wait: 5
--- response_body
true
+
+
+
+=== TEST 16: check for request id in response header when request id is empty
in request
+--- config
+ location /t {
+ content_by_lua_block {
+ local http = require "resty.http"
+ local httpc = http.new()
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port ..
"/opentracing"
+ local res, err = httpc:request_uri(uri,
+ {
+ method = "GET",
+ headers = {
+ ["Content-Type"] = "application/json",
+ ["X-Request-Id"] = ""
+ }
+ })
+
+ if res.headers["X-Request-Id"] and res.headers["X-Request-Id"] ~=
"" then
+ ngx.say("request header present and is not empty")
+ else
+ ngx.say("failed")
+ end
+ }
+ }
+--- response_body
+request header present and is not empty