0

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:


  "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: enter image description here

but when I change the path from "client-service/{**remainder}" to "/{**remainder}", everything works fine! any suggestions on the root of this error?

Update: Here's the 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();

  • I tried to use YARP but your problem did not arise. Can you show your startup.cs configuration? In addition, you can take a look at this article:https://swimburger.net/blog/dotnet/use-yarp-to-host-client-and-api-server-on-a-single-origin – Tupac Dec 09 '21 at 07:09
  • I'm using dotnet 6 so no startup.cs – Sein Gerivani Dec 11 '21 at 05:40

1 Answers1

0

In your sample, the path client-service/url is forwarded as is to the backend. And such url does not exists. That's why removing the prefix works again.

To fix: you need to transform the url to remove the client-service prefix per route. You can do it with yarp adding the PathRemovePrefix transformer:

"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). 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.

For more details on this one, see this issue.

pjmolina
  • 302
  • 4
  • 15