0

How to send an HTTP request to external service in the APISIX Gateway serverless-pre-function, I am trying below but getting an error

 "serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function() ngx.location.capture(\"http://host.docker.internal:9191/health\"); ngx.log(ngx.ERR, \"serverless pre function\"); end"
      ],
      "phase": "before_proxy"
    }
2022/09/13 14:50:20 [error] 49#49: *19282359 open() "/usr/local/apisix/htmlhttp://host.docker.internal:9191/health" failed (2: No such file or directory), client: 172.18.0.1, server: _, request: "GET /dbt/actuator/health HTTP/1.1", subrequest: "http://host.docker.internal:9191/health", host: "127.0.0.1:9080"

Once the external service is called, the pre-function function continue by executing the original request.

ghost
  • 425
  • 4
  • 17

1 Answers1

0

I was able to solve this by using resty.http like below

"serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function(conf, ctx) local core = require(\"apisix.core\")  local httpc = require(\"resty.http\").new() \n local res, err = httpc:request_uri(\"http://host.docker.internal:9191/transform\", {method = \"POST\", body = ngx.req.get_body_data(), headers = { [\"Content-Type\"] = \"application/json\", [\"Upstream\"] = core.request.header(ctx, \"Upstream\"), [\"Transformer\"] = core.request.header(ctx, \"Transformer\")}}) ngx.req.set_header(\"Authorization\", core.request.header(ctx, \"X-Auth\")) ngx.req.init_body() ngx.req.append_body(res.body) ngx.req.finish_body() end"
      ],
      "phase": "before_proxy"
    }
ghost
  • 425
  • 4
  • 17