Skip to content

Commit 946a17e

Browse files
authored
feat(body-transformer): support other data formats without warnings (#10862)
1 parent 75cff5e commit 946a17e

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

apisix/plugins/body-transformer.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ local next = next
3434
local transform_schema = {
3535
type = "object",
3636
properties = {
37-
input_format = { type = "string", enum = {"xml", "json", "encoded", "args"} },
37+
input_format = { type = "string", enum = {"xml", "json", "encoded", "args", "plain"} },
3838
template = { type = "string" },
3939
template_is_base64 = { type = "boolean" },
4040
},
@@ -129,7 +129,7 @@ end
129129
local function transform(conf, body, typ, ctx, request_method)
130130
local out = {}
131131
local format = conf[typ].input_format
132-
if body or request_method == "GET" then
132+
if (body or request_method == "GET") and format ~= "plain" then
133133
local err
134134
if format then
135135
out, err = decoders[format](body)

t/plugin/body-transformer.t

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,3 +1069,61 @@ location /demo {
10691069
assert(res.status == 200)
10701070
}
10711071
}
1072+
1073+
1074+
1075+
=== TEST 16: test for missing Content-Type and skip body parsing
1076+
--- config
1077+
location /demo {
1078+
content_by_lua_block {
1079+
local core = require("apisix.core")
1080+
local body = core.request.get_body()
1081+
assert(body == "{\"message\": \"actually json\"}")
1082+
}
1083+
}
1084+
location /t {
1085+
content_by_lua_block {
1086+
local t = require("lib.test_admin")
1087+
local core = require("apisix.core")
1088+
1089+
local code, body = t.test('/apisix/admin/routes/1',
1090+
ngx.HTTP_PUT,
1091+
string.format([[{
1092+
"uri": "/foobar",
1093+
"plugins": {
1094+
"proxy-rewrite": {
1095+
"uri": "/demo"
1096+
},
1097+
"body-transformer": {
1098+
"request": {
1099+
"input_format": "plain",
1100+
"template": "{\"message\": \"{* string.gsub(_body, 'not ', '') *}\"}"
1101+
}
1102+
}
1103+
},
1104+
"upstream": {
1105+
"type": "roundrobin",
1106+
"nodes": {
1107+
"127.0.0.1:%d": 1
1108+
}
1109+
}
1110+
}]], ngx.var.server_port)
1111+
)
1112+
1113+
if code >= 300 then
1114+
ngx.status = code
1115+
return
1116+
end
1117+
ngx.sleep(0.5)
1118+
1119+
local http = require("resty.http")
1120+
local httpc = http.new()
1121+
local res, err = httpc:request_uri("http://127.0.0.1:" .. ngx.var.server_port .. "/foobar", {
1122+
method = "POST",
1123+
body = "not actually json",
1124+
})
1125+
assert(res.status == 200)
1126+
}
1127+
}
1128+
--- no_error_log
1129+
no input format to parse

0 commit comments

Comments
 (0)