Envoy lua filter - parse http json response

2,430 views
Skip to first unread message

Mogos Sebastian

unread,
Jul 6, 2018, 12:56:17 PM7/6/18
to envoy-dev
When using an envoy.lua http filter, is there a way to parse the json body response of a request executed from within the envoy_on_request function:
local headers, body = request_handle:httpCall

Thanks

d...@rockybars.com

unread,
Jul 6, 2018, 7:40:04 PM7/6/18
to envoy-dev
Yes, you can take advantage of e.g. this Lua library: https://github.com/rxi/json.lua

For example, you have the following cluster in your config:

- name: service_bin
  connect_timeout: 25s
  type: LOGICAL_DNS
  dns_lookup_family: V4_ONLY
  lb_policy: ROUND_ROBIN
  hosts: [{ socket_address: { address: httpbin.org, port_value: 443 }}]
  tls_context: { sni: httpbin.org }


Your Lua script could be, note that you can always override the Lua package path instead of the default one: http://lua-users.org/wiki/PackagePath

function envoy_on_request(request_handle)
  -- overrides package.path
  package.path = package.path .. ";/path/to/json.lua"
  json = require "json"
  local headers, body = request_handle:httpCall(
    "service_bin",
    {
      [":method"] = "GET",
      [":path"] = "/ip",
      [":authority"] = "httpbin.org"
    },
    "",
    5000)
    request_handle:logInfo(json.decode(body)["origin"])
end

Mogos Sebastian

unread,
Jul 7, 2018, 10:50:31 AM7/7/18
to envoy-dev
Perfect, thank you!
Reply all
Reply to author
Forward
0 new messages