首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >APISIX Serverless中对外部服务的中间HTTP调用

APISIX Serverless中对外部服务的中间HTTP调用
EN

Stack Overflow用户
提问于 2022-09-13 14:52:28
回答 1查看 75关注 0票数 0

如何将HTTP请求发送到APISIX网关无服务器预功能中的外部服务,我在下面尝试,但得到了一个错误

代码语言:javascript
复制
 "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"
    }
代码语言:javascript
复制
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"

一旦调用外部服务,预函数函数将通过执行原始请求继续执行。

EN

回答 1

Stack Overflow用户

发布于 2022-09-14 15:23:57

我能够通过如下所示的resty.http来解决这个问题

代码语言:javascript
复制
"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"
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73705086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档