Envoy Filter with a Lua script to fetch data from other API

I’ve got a Envoy Filter in which I add a header to every HTTP request. The header’s value comes from API.

Let’s assume two configurations of the filter. In the configuration below I added a hardcoded version of my header. It was checked in the logs of my target application and it works.

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
  name: lua-filter
  - applyTo: HTTP_FILTER
      context: ANY
        portNumber: 7123
            name: "envoy.http_connection_manager"
              name: "envoy.router"
      operation: INSERT_BEFORE
       name: envoy.lua
         "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
         inlineCode: |
            function envoy_on_request(request_handle)
                request_handle:headers():add("authorization", "it works!")

This time I want to have the header’s value coming from my API. Unfortunately, this setup doesn’t work and I have no idea why. I have checked the Lua script on my local machine, and the script itself works but as soon as I provide the script to the filter, no header is added. Can anyone know what might be the reason this doesn’t add the header? Or at least provide me some details how to debug through envoy filter?

     "@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
     inlineCode: |
        function envoy_on_request(request_handle)
          local http = require('socket.http')
          local json = require('json')
          local ltn12 = require "ltn12"
          local reqbody="my request body"
          local respbody = {} 
          local  body, code, headers, status = http.request {
          method = "POST",
          url = "http://my-address",
          source = ltn12.source.string(reqbody),
          headers = 
          ["Accept"] = "*/*",
          ["Accept-Encoding"] = "gzip, deflate",
          ["Accept-Language"] = "en-us",
          ["Content-Type"] = "application/x-www-form-urlencoded",
          ["content-length"] = string.len(reqbody)
          sink = ltn12.sink.table(respbody)
          respbody = table.concat(respbody)
          parsed = json.decode(respbody)
          token = parsed["token-value"]

not sure if its still relevant but will try to help anyway.

First in order to debug your Envoy you can update the level for each filter to debug.
assuming your pod is myapp-123 that will be done like the following:
istioctl proxy-config log myapp-123 --level lua:debug jwt:debug

i would assume you probably will see an error regarding the json object which is nill. not sure its possible to add any module you want to the EnvoyProxy.
this should be added somehow on the EnvoyProxy itself, like installing that module.