vaibhavsw opened a new issue, #7915:
URL: https://github.com/apache/apisix/issues/7915

   ### Description
   
   I have written a transformer that transforms the HTTP request body in 
`serverless-pre-function` in the `before-proxy` phase
   
   ```JSON
   "serverless-pre-function": {
         "disable": false,
         "functions": [
           "return function(conf, ctx) local core = require(\"apisix.core\")  
local httpc = require(\"resty.http\").new() \n ngx.req.read_body() \n local 
res, err = httpc:request_uri(\"http://host.docker.internal:9191/transform\";, 
{method = \"POST\", body = ngx.req.get_body_data(), headers = { 
[\"Content-Type\"] = \"application/json\", [\"Upstream\"] = 
core.request.header(ctx, \"Upstream\"), [\"Transformer\"] = 
core.request.header(ctx, \"Transformer\")}}) ngx.req.set_body_data(res.body) 
end"
         ],
         "phase": "before_proxy"
       }
   ```
   
   However, when this is executed, it is throwing below error.
   
   ```bash
   2022/09/14 06:50:39 [error] 46#46: *1287423 lua entry thread aborted: 
runtime error: [string "return function(conf, ctx) local core = requi..."]:3: 
attempt to call field 'set_body_data' (a string value)
   stack traceback:
   coroutine 0:
        [string "return function(conf, ctx) local core = requi..."]: in 
function 'func'
        /usr/local/apisix/apisix/plugins/serverless/init.lua:84: in function 
'phase_func'
        /usr/local/apisix/apisix/plugin.lua:897: in function 'common_phase'
        /usr/local/apisix/apisix/init.lua:555: in function 'http_access_phase'
        access_by_lua(nginx.conf:284):2: in main chunk, client: 172.18.0.1, 
server: _, request: "POST /card/balance HTTP/1.1", host: "127.0.0.1:9080"
   ```
   
   The expectation is to override the client request body with the transformed 
request body after accessing the external service with resty.http and then that 
must be sent to the upstream.
   
   ### Environment
   
   - APISIX version (run `apisix version`): 2.15
   - Operating system (run `uname -a`): MacOS
   - OpenResty / Nginx version (run `openresty -V` or `nginx -V`): nginx 
version: openresty/1.21.4.1
   - etcd version, if relevant (run `curl 
http://127.0.0.1:9090/v1/server_info`): 3.4.15
   - APISIX Dashboard version, if relevant:
   - Plugin runner version, for issues related to plugin runners:
   - LuaRocks version, for installation issues (run `luarocks --version`): 3.8.0
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to