【问题标题】:Ocelot post request not going throughOcelot 发布请求未通过
【发布时间】:2020-09-16 14:45:48
【问题描述】:

我在我的项目中实现了 Ocelot API 网关,该网关对“GET”请求运行良好,但是当我尝试发送“POST”请求时出现错误:

previousRequestId:没有以前的请求 ID,消息:错误代码:UnableToFindDownstreamRouteError 消息:无法匹配上游路径的 ReRoute 配置:/api/patient/CreateAppointment,动词:POST。在 ResponderMiddleware 中发现错误。请求路径设置错误响应:/api/patient/CreateAppointment,请求方法:POST

以下是我的ocelot.json:

{
  "Routes": [
    {
      "DownstreamPathTemplate": "/api/patient/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "patientservice",
          "Port": 81
        }
      ],
      "UpstreamPathTemplate": "/api/patient/{everything}",
      "UpstreamHttpMethod": [ "GET", "POST" ],
      "UpstreamHost": "*"
    },
    {
      "DownstreamPathTemplate": "/api/actor",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "postgresqldapper",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/api/actor",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/product/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "productservice",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/api/product/{everything}",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/weatherforecast",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "postgresqldapper",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/api/weatherforecast",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/user",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "loginservicedapr",
          "Port": 80
        }
      ],
      "UpstreamPathTemplate": "/api/user",
      "UpstreamHttpMethod": [ "GET" ]
    },
    {
      "DownstreamPathTemplate": "/api/user/{everything}",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "loginservicedapr",
          "Port": 5001
        }
      ],
      "UpstreamPathTemplate": "/api/user/{id}",
      "UpstreamHttpMethod": [ "GET" ]
    }

  ],
  "GlobalConfiguration": {
    "RequestIdKey": "OcRequestId",
    "AdministrationPath": "/administration"
  }
}

当直接从 Swagger 或 Postman 调用时,我执行“POST”请求的 API 对于“POST”请求运行良好。

请让我知道我应该在 ocelot.json 文件中更改什么以便“POST”请求可以通过?

【问题讨论】:

    标签: .net-core microservices asp.net-core-webapi ocelot


    【解决方案1】:

    您的错误实际上是说 Ocelot 无法将请求路由到

    POST /api/patient/CreateAppointment
    

    在您的屏幕截图中,您的 curl 命令(正在运行)是一个请求:

    POST /api/patient
    

    您的/{everything} 路径后缀告诉 Ocelot,无论您调用网关使用的任何后缀都将出现在下游服务中。

    我的理论是您没有在下游患者服务 API 上定义 CreateAppointment 端点操作。在您的服务上定义此路径后,您的 /api/patient/{everything} 路由映射应该可以正常工作。

    【讨论】:

    • 但我在重新路由中添加了“/api/patient/{everything}”。那应该照顾它吧?在 ocelot.json 中为每条路线添加路线是很困难的。请建议我该如何处理?
    • 对不起,如果不清楚。我要说的是,您没有从 swagger UI 中点击创建约会端点。如果您使用邮递员或 curl 并尝试调用 /createappointment 它将不起作用
    猜你喜欢
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 2019-01-12
    • 2017-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多