apache / apisix

The Cloud-Native API Gateway
https://apisix.apache.org/blog/
Apache License 2.0
14.3k stars 2.49k forks source link

help request: how to use body-transformer with a multi-tag template #10262

Open coco961003 opened 11 months ago

coco961003 commented 11 months ago

Description

As mentioned in title, I tried two ways:

  1. The template like this:

    {
    "plugins": {
        "body-transformer": {
            "request": {
                "template": "{%local a = \"hello\"%}{{{a}}:\"world\"}"
            }
        }
    }
    }

    The rsp is

    {"message":"request template compile: /usr/local/apisix//deps/share/lua/5.1/resty/template.lua:389: [string \"context=... or {}...\"]:6: '}' expected near ')'"}
  2. encode the file with base64

    [root@localhost ~]# cat test1.txt
    {%
    local a = "hello"
    %}
    {
    {{a}}:"world"
    }
    [root@localhost ~]# base64 -w0 test1.txt
    eyUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K
    {
    "plugins": {
        "body-transformer": {
            "request": {
                "template": "eyUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"
            }
        }
    }
    }

    The result is image It is stuck. About one minute later, return timeout message.

The log is (see the output the template render in the log):

2023/09/26 10:03:41 [info] 50#50: *104184 [lua] init.lua:638: http_access_phase(): matched route: {"clean_hndlers":{},"orig_modifiedIndex":479,"createdIndex":164,"value":{"priority":0,"upstream":{"nodes":[{"priorit":0,"host":"172.16.20.208","weight":1,"port":9004,"upstream_host":"172.16.20.208:9004"}],"parent":{"clean_hndlers":{},"orig_modifiedIndex":479,"createdIndex":164,"value":{"priority":0,"upstream":{"nodes":[{"port":904,"host":"172.16.20.208","weight":1}],"hash_on":"vars","pass_host":"pass","scheme":"http","parent":"table:0x7fab1e873470","type":"roundrobin"},"plugins":{"body-transformer":{"request":{"input_format":"json","templte":"eyUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"}}},"status":1,"id":"test-body-transfrmer","create_time":1695349129,"uri":"/*","update_time":1695722306,"host":"test-body-transformer.com"},"modfiedIndex":479,"key":"/apisix/routes/test-body-transformer","has_domain":false},"hash_on":"vars","type":"rondrobin","original_nodes":[{"port":9004,"host":"172.16.20.208","weight":1}],"scheme":"http","nodes_ref":"tale: 0x7fab1e862de8","pass_host":"pass"},"plugins":{"body-transformer":{"request":{"input_format":"json","teplate":"eyUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"}}},"update_time":1695722306,"id":test-body-transformer","create_time":1695349129,"uri":"/*","host":"test-body-transformer.com","status":1},"as_domain":false,"key":"/apisix/routes/test-body-transformer","modifiedIndex":479}, client: 192.168.137.254 server: _, request: "POST /test/echo HTTP/1.1", host: "test-body-transformer.com"
2023/09/26 10:03:41 [info] 50#50: *104184 [lua] body-transformer.lua:157: transform(): request body transfom output={
  hello:"world"
}
, client: 192.168.137.254, server: _, request: "POST /test/echo HTTP/1.1", host: "test-body-transformer.com
2023/09/26 10:03:41 [info] 50#50: *104184 [lua] balancer.lua:195: pick_server(): route: {"clean_handlers":{,"orig_modifiedIndex":479,"createdIndex":164,"value":{"priority":0,"upstream":{"nodes":[{"priority":0,"host:"172.16.20.208","weight":1,"port":9004,"upstream_host":"172.16.20.208:9004"}],"parent":{"clean_handlers":{,"orig_modifiedIndex":479,"createdIndex":164,"value":{"priority":0,"upstream":{"nodes":[{"port":9004,"host""172.16.20.208","weight":1}],"hash_on":"vars","pass_host":"pass","scheme":"http","parent":"table: 0x7fab1e83470","type":"roundrobin"},"plugins":{"body-transformer":{"request":{"input_format":"json","template":"eyUKCAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"}}},"status":1,"id":"test-body-transformer","crate_time":1695349129,"uri":"/*","update_time":1695722306,"host":"test-body-transformer.com"},"modifiedIndex:479,"key":"/apisix/routes/test-body-transformer","has_domain":false},"hash_on":"vars","type":"roundrobin",original_nodes":[{"port":9004,"host":"172.16.20.208","weight":1}],"scheme":"http","nodes_ref":"table: 0x7fa1e862de8","pass_host":"pass"},"plugins":{"body-transformer":{"request":{"input_format":"json","template":"eUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"}}},"update_time":1695722306,"id":"test-bodytransformer","create_time":1695349129,"uri":"/*","host":"test-body-transformer.com","status":1},"has_domain:false,"key":"/apisix/routes/test-body-transformer","modifiedIndex":479}, client: 192.168.137.254, server: , request: "POST /test/echo HTTP/1.1", host: "test-body-transformer.com"
2023/09/26 10:03:41 [info] 50#50: *104184 [lua] balancer.lua:196: pick_server(): ctx: {"route_id":"test-bod-transformer","plugins":[{"name":"body-transformer","body_filter":"function: 0x7fab20e97808","header_filter:"function: 0x7fab20e97788","priority":1080,"schema":{"properties":{"_meta":{"properties":{"priority":{"desription":"priority of plugins by customized order","type":"integer"},"error_response":{"oneOf":[{"type":"sting"},{"type":"object"}]},"disable":{"type":"boolean"},"filter":{"description":"filter determines whether te plugin needs to be executed at runtime","type":"array"}},"type":"object"},"response":"table: 0x7fab4c023f0","request":{"properties":{"input_format":{"enum":["xml","json"],"type":"string"},"template":{"type":"strig"}},"required":["template"],"type":"object"}},"type":"object","$comment":"this is a mark for our injected lugin schema","anyOf":[{"required":["request"]},{"required":["response"]},{"required":["request","response"}]},"rewrite":"function: 0x7fab4f895688","check_schema":"function: 0x7fab4c043470","version":0.1},{"request:{"input_format":"json","template":"eyUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"}}],"cnf_version":479,"matched_route":{"clean_handlers":{},"orig_modifiedIndex":479,"createdIndex":164,"value":{"riority":0,"upstream":{"nodes":[{"priority":0,"host":"172.16.20.208","weight":1,"port":9004,"upstream_host""172.16.20.208:9004"}],"parent":{"clean_handlers":{},"orig_modifiedIndex":479,"createdIndex":164,"value":{"riority":0,"upstream":{"nodes":[{"port":9004,"host":"172.16.20.208","weight":1}],"hash_on":"vars","pass_hos":"pass","scheme":"http","parent":"table: 0x7fab1e873470","type":"roundrobin"},"plugins":{"body-transformer:{"request":{"input_format":"json","template":"eyUKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQin0K"}}},"status":1,"id":"test-body-transformer","create_time":1695349129,"uri":"/*","update_time":169572230,"host":"test-body-transformer.com"},"modifiedIndex":479,"key":"/apisix/routes/test-body-transformer","has_omain":false},"hash_on":"vars","type":"roundrobin","original_nodes":[{"port":9004,"host":"172.16.20.208","wight":1}],"scheme":"http","nodes_ref":"table: 0x7fab1e862de8","pass_host":"pass"},"plugins":{"body-transforer":"table: 0x7fab1e860a20"},"update_time":1695722306,"id":"test-body-transformer","create_time":1695349129"uri":"/*","host":"test-body-transformer.com","status":1},"has_domain":false,"key":"/apisix/routes/test-bod-transformer","modifiedIndex":479},"body_transformer_conf":{"request":{"input_format":"json","template":"eyKICAgIGxvY2FsIGEgPSAiaGVsbG8iCiV9CnsKICB7e2F9fToid29ybGQiCn0K"}},"conf_id":"test-body-transformer","curr_re_matched":{"_path":"/*","_host":"test-body-transformer.com",":ext":"test/echo","_method":"POST"},"conf_type:"route","matched_upstream":"table: 0x7fab1e860788","upstream_version":"479#table: 0x7fab1e860788","upstrea_key":"test-body-transformer","upstream_scheme":"http","upstream_conf":"table: 0x7fab1e860788","var":{"_reqest":"cdata<void *>: 0x55b4f4a745b0","_cache":{"scheme":"http","http_content_type":"application/json","host:"test-body-transformer.com","request_uri":"/test/echo","uri":"/test/echo","upstream_scheme":"http","real_rquest_uri":"/test/echo"},"_ctx":{"route_id":"test-body-transformer","plugins":"table: 0x7fab4fa0e460","confversion":479,"matched_route":"table: 0x7fab1e8605e8","body_transformer_conf":"table: 0x7fab1e7c99c8","conf_d":"test-body-transformer","curr_req_matched":"table: 0x7fab1e7c65c0","conf_type":"route","matched_upstream:"table: 0x7fab1e860788","upstream_version":"479#table: 0x7fab1e860788","upstream_key":"test-body-transformr","upstream_scheme":"http","upstream_conf":"table: 0x7fab1e860788","var":"table: 0x7fab1ed3bb68"}}}, clien: 192.168.137.254, server: _, request: "POST /test/echo HTTP/1.1", host: "test-body-transformer.com"

Why it is stuck when I used Base64 mode and what is the best practice for the template with multi tags (one string contains {{expression}}, {expression}, {% lua code %} etc.).

Thanks.

Environment

monkeyDluffy6017 commented 9 months ago

@coco961003 have you solved your problem?

coco961003 commented 8 months ago

@monkeyDluffy6017

No, so much time cost that my company let me do other work. If u have any tips, plz tell me.