Hello,

I am trying to use the body-transformer
<https://apisix.apache.org/docs/apisix/plugins/body-transformer/> plugin in
order to alter a response (JSON) body but without success.
Here's the code I am using:
res_template=$(cat <<EOF
{%
cjson = require(\"cjson.safe\")
responseBodyJson = cjson.decode(_body)
responseBodyJson.newAttribute = \"new attribute\"
modifiedBody = cjson.encode(responseBodyJson)
%}
{{\"Hello\"}}
{{ngx.arg[1]}}
{{ngx.arg[2]}}
{{modifiedBody}}
{{\"Bye\"}}
EOF
)

echo "Creating Body Transformer Plugin"
plugin_config_payload='{
"desc": "Body Transformation Plugin",
"plugins": {
"body-transformer": {
"response": {
"input_format": "json",
"template": "'"$res_template "'"
}
}
}
}'

With this code, the body is indeed replaced and becomes:
"Hello true null Bye"
Which tells us that the ngx first argument is empty and the second argument
shows a true boolean flag (the _body variable is probably nil or empty as
well).
It seems as though I am trying to access the body at a point where it is no
longer there but then, I am using the body-transformer so I'd hope I'd be
able to manipulate the body with it.
I also tried accessing the ctx, _ctx, _ctx.var, ctx.var.rsp_body and other
variations with no success.
Can you help please?
Environment

   - APISIX version: 3.0.1;
   - Operating system: Linux fa074110730d 5.15.0-73-generic ~20.04.1-Ubuntu
   SMP Wed May 17 14:58:14 UTC 2023 x86_64 GNU/Linux (docker);
   - OpenResty / Nginx version: openresty/1.21.4.1;


Regards,
Emil Xavier

Reply via email to