Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: body-transformer plugin for other data formats without warnings #10862

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apisix/plugins/body-transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local next = next
local transform_schema = {
type = "object",
properties = {
input_format = { type = "string", enum = {"xml", "json", "encoded", "args"} },
input_format = { type = "string", enum = {"xml", "json", "encoded", "args", "plain"} },
template = { type = "string" },
template_is_base64 = { type = "boolean" },
},
Expand Down Expand Up @@ -129,7 +129,7 @@ end
local function transform(conf, body, typ, ctx, request_method)
local out = {}
local format = conf[typ].input_format
if body or request_method == "GET" then
if (body or request_method == "GET") and format ~= "plain" then
local err
if format then
out, err = decoders[format](body)
Expand Down
58 changes: 58 additions & 0 deletions t/plugin/body-transformer.t
Original file line number Diff line number Diff line change
Expand Up @@ -1069,3 +1069,61 @@ location /demo {
assert(res.status == 200)
}
}



=== TEST 16: test for missing Content-Type and skip body parsing
--- config
location /demo {
content_by_lua_block {
local core = require("apisix.core")
local body = core.request.get_body()
assert(body == "{\"message\": \"actually json\"}")
}
}
location /t {
content_by_lua_block {
local t = require("lib.test_admin")
local core = require("apisix.core")

local code, body = t.test('/apisix/admin/routes/1',
ngx.HTTP_PUT,
string.format([[{
"uri": "/foobar",
"plugins": {
"proxy-rewrite": {
"uri": "/demo"
},
"body-transformer": {
"request": {
"input_format": "plain",
"template": "{\"message\": \"{* string.gsub(_body, 'not ', '') *}\"}"
}
}
},
"upstream": {
"type": "roundrobin",
"nodes": {
"127.0.0.1:%d": 1
}
}
}]], ngx.var.server_port)
)

if code >= 300 then
ngx.status = code
return
end
ngx.sleep(0.5)

local http = require("resty.http")
local httpc = http.new()
local res, err = httpc:request_uri("http://127.0.0.1:" .. ngx.var.server_port .. "/foobar", {
method = "POST",
body = "not actually json",
})
assert(res.status == 200)
}
}
--- no_error_log
no input format to parse
Loading