To avoid this error and return a field with a null value in the response body, we need to make a few updates to the environment variables and then change the Lua slightly to require cjson.
First, set the following environment variables:
KONG_UNTRUSTED_LUA: "sandbox"
KONG_UNTRUSTED_LUA_SANDBOX_REQUIRES: "cjson"
Review the untrusted Lua configuration reference before updating this in your environment.
Next, let’s add the following to our Exit Transformer.
Saved this to a file called bodyNull.lua:
return function(status, body, headers)
if status > 399 then
body = { hello = (require "cjson").null }
end
return status, body, headers
end
Then we need to create the Exit Transformer plugin:
curl -X POST 'http://localhost:8001/plugins' \
--header 'Kong-Admin-Token: <token>' \
--form 'config.functions=@"/<pathtofile>/bodyNull.lua"' \
--form 'name="exit-transformer"'
For this specific test I created a request-termination plugin set to a status of 400 to trigger the Lua from the Exit Transformer.
curl -X POST http://localhost:8001/plugins \
--header 'Kong-Admin-Token: <token>' \
--data "name=request-termination" \
--data "config.status_code=400" \
Now if we run a request we can see that the field hello is returned with a null value: