使用 Lua 脚本配置 Krakend

我编写了一个简单的 Krakend.json 配置来配置我的终端点。该配置调用了一个 Lua 脚本,目前它只打印出响应体。

{
      "version": 2,
      "extra_config": {
        "github_com/devopsfaith/krakend-gologging": {
          "level":  "DEBUG",
          "prefix": "[KRAKEND]",
          "syslog": false,
          "stdout": true
        },
        "github_com/devopsfaith/krakend-metrics": {
          "collection_time": "60s",
          "proxy_disabled": false,
          "router_disabled": false,
          "backend_disabled": false,
          "endpoint_disabled": false,
          "listen_address": ":8090"
        }
      },
      "timeout": "3000ms",
      "cache_ttl": "300s",
      "output_encoding": "json",
      "name": "myregistrydomain.com",
      "endpoints": [
        {
          "endpoint": "/models/{group-id}",
          "method": "GET",
          "output_encoding": "json",
            "headers_to_pass": [
            "Authorization",
            "Content-Type"
          ],

          "backend": [
            {
              "url_pattern": "myregistrydomain/get-models-by-group/group-id/{group-id}",
              "timeout": "10s",
              "encoding": "no-op",
              "sd": "static",
              "host": ["myregistrydomain"],
              "disable_host_sanitize": "false",
              "extra_config": {
                  "github.com/devopsfaith/krakend/http": {
                      "return_error_details": "backend_models"
                  },
                  "github.com/devopsfaith/krakend-lua/proxy/backend": {
                      "sources": ["file1.lua"],
                      "post": "check(response);",
                      "live": true,
                      "allow_open_libs": true,
                      "skip_next": false
                  }
              }
            }
          ],
          "querystring_params": [
            "{group-id}"
          ]
        }
      ]
    }

Lua 函数如下:

function check(response)
    print("**")
    local r = response.load()
    print(r:body())
end

目前 print body 可以正常工作,我可以看到所有的 JSON 响应。但是在我的 postman 请求中,我看到一个空的响应体。我想仅仅使用 Lua 函数来检查响应体是否包含字符串 “error”。

我的主要问题是为什么日志中可以看到响应体而在 postman 中我的响应体为空?

原文链接 https://stackoverflow.com/questions/71081217

点赞