简体   繁体   English

Swagger 从 YARP 代理服务器访问时显示“无法加载 API 定义”错误

[英]Swagger shows "Failed to load API definition" error when accessing from YARP proxy server

Since ocelot is no longer maintained, I decided to give a go to YARP as my API Gateway but it's giving me this silly error when I access one of my endpoints through it:由于不再维护 ocelot,我决定将 go 作为我的 API 网关提供给 YARP,但是当我通过它访问我的端点之一时,它给了我这个愚蠢的错误:


  "ReverseProxy": {
    "Routes": {
      "client-route": {
        "ClusterId": "client-cluster",
        "CorsPolicy": "customPolicy",
        "Match": {
          "Path": "client-service/{**remainder}"
        },
        "Transforms": [
          { "X-Forwarded": "Off" },
          {
            "PathPattern": "/{**remainder}"
          }
        ]
      }
    },
    "Clusters": {
      "client-cluster": {
        "Destinations": {
          "destination1": {
            "Address": "https://URLWithSwagger/"
          }
        }
      }
    }
  }

when using the configuration above, I get the following error:使用上面的配置时,出现以下错误: 在此处输入图像描述

but when I change the path from "client-service/{**remainder}" to "/{**remainder}" , everything works fine?但是当我将路径从"client-service/{**remainder}"更改为"/{**remainder}"时,一切正常吗? any suggestions on the root of this error?关于这个错误的根源有什么建议吗?

Update: Here's the program.cs:更新:这是 program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddReverseProxy()//.AddConfigFilter();
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));

var app = builder.Build();

app.UseHttpsRedirection();

app.UseAuthorization();


app.MapReverseProxy();

app.Run();

In your sample, the path client-service/url is forwarded as is to the backend.在您的示例中,路径client-service/url按原样转发到后端。 And such url does not exists.而这样的 url 是不存在的。 That's why removing the prefix works again.这就是为什么删除前缀再次起作用的原因。

To fix: you need to transform the url to remove the client-service prefix per route.要修复:您需要转换 url 以删除每个路由的client-service前缀。 You can do it with yarp adding the PathRemovePrefix transformer:您可以通过添加PathRemovePrefix转换器的yarp来实现:

"client-route": {
    "ClusterId": "client-cluster",
    "Match": {
      "Path": "/client-service/{****remainder}"
    },
    "Transforms": [
      { "PathRemovePrefix": "/client-service" }
    ]
  },
      

This will work to get the raw swagger Specification (JSON file).这将用于获取原始 swagger 规范(JSON 文件)。 If you want to render it on SwaggerUI you should inject SwaggerUI on the reverse-proxy and pass the forwarded contract: openapi.json URI to it.如果你想在 SwaggerUI 上呈现它,你应该在反向代理上注入 SwaggerUI 并将转发的合同:openapi.json URI 传递给它。

For more details on this one, see this issue .有关这方面的更多详细信息,请参阅此问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Blazor Swagger:无法加载 API 定义。 获取错误:未定义 /swagger/v1/swagger.json - Blazor Swagger: Failed to load API definition. Fetch error: undefined /swagger/v1/swagger.json 未能加载 API 定义,获取错误:未定义 /swagger/v1/swagger.json - Failed to load API definition, Fetch error: undefined /swagger/v1/swagger.json Swashbuckle/Swagger + ASP.Net Core:“无法加载 API 定义” - Swashbuckle/Swagger + ASP.Net Core: "Failed to load API definition" swagger:无法加载未定义的 API 定义 /swagger/v1/swagger.json - swagger : Failed to load API definition undefined /swagger/v1/swagger.json 当 function 返回文件时,无法加载 API 定义 - Failed to load API definition when a function returns a file Yarp 代理不记录重定向? - Yarp proxy does not log a redirect? Yarp 反向代理 - 响应 502 错误网关 - Yarp Reverse Proxy - Response 502 Bad Gateway 加载资源失败:服务器响应状态为 500(内部服务器错误) - Web API 托管在 IIS - Failed to load resource: the server responded with a status of 500 (Internal Server Error) - Web API hosted on IIS 访问部署到AWS API网关的AWS无服务器API时返回内部服务器错误 - Internal Server Error returned when accessing aws Serverless API deployed to AWS API gateway 如何修复“无法加载资源:服务器响应状态为400()”API获取错误 - How to fix “Failed to load resource: the server responded with a status of 400 ()” API fetch error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM