EnvoyFilter Lua json encode

Hello guys,

How can i encode a json inside a envoyfilter?

function envoy_on_request(request_handle)

              path = get_header(request_handle, ":path")
              local host = get_header(request_handle, ":authority")

              local auth_host = "dev-auth.services.svc.cluster.local"
              local auth_path = "/api/v1/has-permission"

              local cluster = "outbound|8080||" .. auth_host

              local request_headers = {
                  [":method"] = "POST",
                  [":path"] = auth_path,
                  [":authority"] = auth_host,
                  ["Content-Type"] = "application/json"

              }

              local jsonFile = {
                  ["application"] = "APP",
                  ["path"] = "/",
                  ["method"] = "GET"
              }

              local request_body = json.encode(jsonFile)

This doesnt work, is there any special library?

Thanks

yes. you have to load some lua library e.g. dkjson from the volume mounted in istio-proxy in global scope and use some external library like

        json = (loadfile "/var/lib/lua/dkjson")() 

function envoy_on_request(request_handle)

          path = get_header(request_handle, ":path")

1 Like