This is an automated email from the ASF dual-hosted git repository.
monkeydluffy 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 946a17ea3 feat(body-transformer): support other data formats without
warnings (#10862)
946a17ea3 is described below
commit 946a17ea3e07fb729b2acacb00e50521eb51d620
Author: baiyun <[email protected]>
AuthorDate: Thu Jan 25 16:24:10 2024 +0800
feat(body-transformer): support other data formats without warnings
(#10862)
---
apisix/plugins/body-transformer.lua | 4 +--
t/plugin/body-transformer.t | 58 +++++++++++++++++++++++++++++++++++++
2 files changed, 60 insertions(+), 2 deletions(-)
diff --git a/apisix/plugins/body-transformer.lua
b/apisix/plugins/body-transformer.lua
index 368ccf9de..977ebce67 100644
--- a/apisix/plugins/body-transformer.lua
+++ b/apisix/plugins/body-transformer.lua
@@ -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" },
},
@@ -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)
diff --git a/t/plugin/body-transformer.t b/t/plugin/body-transformer.t
index 33b9b2261..929ed1aac 100644
--- a/t/plugin/body-transformer.t
+++ b/t/plugin/body-transformer.t
@@ -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