Krakend can not use NoOp encoding with more than one backends connected to the same endpoint

130 views Asked by At

Environment info:

  • KrakenD version: watch
  • System info: docker
  • Backend technology: Java, JsonApi.
  • Additional environment information: WSL

Describe what are you trying to do: i need to use sequential proxy after modified response of the first request. I have to use no-op when a set the body of the response.

Your configuration file:

{
  "$schema": "https://www.krakend.io/schema/v3.json",
  "version": 3,
  "port": 9000,
  "timeout": "300000s",
  "cache_ttl": "4000s",
  "client_tls": {
    "allow_insecure_connections": true
  },
  "extra_config": {
    "router": {
      "return_error_msg": true
    }
  },
  "endpoints": [
{
      "endpoint": "/path/to/identification/{id}",
      "input_query_strings": [
        "include"
      ],
      "method": "GET",
      "backend": [
        {
          "host": [
            "https://test.api_1"
          ],
          "url_pattern": "/path/to/identification/{id}"
          "encoding": "no-op",
          "extra_config": {
            "modifier/lua-backend": {
              "sources": [
                "/etc/krakend/json_modifier.lua"
              ],
              "post": "local r = response.load(); modifyBody(r);",
              "live": true,
              "allow_open_libs": true
            }
          }
        },
        {
          "host": [
            "https://test_api_2"
          ],
          "encoding": "no-op",
          "url_pattern": "/path/to/{resp0_data.id}"
        }
      ],
      "extra_config": {
        "proxy": {
          "sequential": true
        }
      }
    }
]
}

My Lua script:

function modifyBody(response)
    local parsedJson = json.decode(response:body())
    -- some logics
    local newBody = ...
    response:body(newBody) 
    response:headers("Content-Length", tostring(string.len(newBody)))
end

Commands used:

version: "3"
services:
  krakend_ce:
    image: devopsfaith/krakend:watch
    volumes:
      - ./krakend:/etc/krakend
    ports:
      - "9000:9000"
    command: ["run", "-d", "-c", "/etc/krakend/krakend.json"]

 docker-compose -f docker-compose.yml up -d

Logs:

 ^C2023/10/26 08:35:55 Signal intercepted: interrupt
[00] 2023/10/26 08:35:55 KRAKEND INFO: [SERVICE: Gin] Router execution ended
[00] Starting service
[00] ERROR parsing the configuration file: '/etc/krakend/krakend.json': can not use NoOp encoding with more than one backends connected to the same endpoint
[00] Killing service

Additional comments:

1

There are 1 answers

2
Albert Garcia On

That's the expected behavior: no-op encoding is only allowed on single-backend endpoints.

You have two possible approaches to implement this:

a) If your backend is returning JSON, use json encoding instead of no-op, and use the data getter (which contains the parsed response from the backend: https://www.krakend.io/docs/endpoints/lua/#response-functions-response) instead of body getter, to tailor the needed response.

b) create an individual internal no-op endpoint to do that body response manipulation with Lua, and then point to that internal endpoint in your sequential-proxy endpoint.