Ocelot
Ocelot copied to clipboard
Can't find route to Downstream API
Hi,
Having an issue with Ocelot routing to downstream API. Appreciate any help.
APIs used in test
Upstream API : https://localhost:7003;http://localhost:5003 Downstream API : https://localhost:3001;http://localhost:3000
Using .Net 6
The Exception
requestId: 0HMJFT6TRQ7L7:00000002, previousRequestId: no previous request id, message: Upstream url path is /Carriers
warn: Ocelot.DownstreamRouteFinder.Middleware.DownstreamRouteFinderMiddleware[0] requestId: 0HMJFT6TRQ7L7:00000002, previousRequestId: no previous request id, message: DownstreamRouteFinderMiddleware setting pipeline errors. IDownstreamRouteFinder returned Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /Carriers, verb: POST. warn: Ocelot.Responder.Middleware.ResponderMiddleware[0] requestId: 0HMJFT6TRQ7L7:00000002, previousRequestId: no previous request id, message: Error Code: UnableToFindDownstreamRouteError Message: Failed to match Route configuration for upstream path: /Carriers, verb: POST. errors found in ResponderMiddleware. Setting error response for request path:/Carriers, request method: POST dbug: Ocelot.Errors.Middleware.ExceptionHandlerMiddleware[0] requestId: 0HMJFT6TRQ7L7:00000002, previousRequestId: no previous request id, message: ocelot pipeline finished
Upstream Setup (Ocelot installed here)
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Identity.Web; using Ocelot.DependencyInjection; using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders(); builder.Logging.AddConsole();
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT"); builder.Configuration.SetBasePath(Directory.GetCurrentDirectory() + "\Configuration") .AddJsonFile("ocelot." + environment + ".json", false, true) .AddJsonFile("appsettings." + environment + ".json") .AddEnvironmentVariables();
Console.WriteLine("Setting up Reverse Proxy");
builder.Services.AddOcelot(builder.Configuration); var app = builder.Build();
app.UseOcelot().Wait();
app.Run();
ocelot.local.json : Note I've used the Reroutes and Route element, both failed.
{
"ReRoutes": [
{
"DownstreamPathTemplate": "/api/{everything}",
"DownstreamScheme": "https",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 3001
}
],
"UpstreamPathTemplate": "/{everything}",
"UpstreamHttpMethod": [ "Post" ]
},
],
"GlobalConfiguration": {
"BaseUrl": "https://localhost:7003",
}
}
Downstream API Controller
[Route("api/[controller]")]
[ApiController]
public class CarrierAdminController : BaseController
{
[HttpGet("Carriers")]
public async Task<IEnumerable<CarrierSDK.Carrier>> GetCarriersAsync()
{
// etc etc
}
}
HttpRequestMessage sent to Upstream API from test.
AbsoluteUri "https://localhost:7003/Carriers"
Found the issue. Downstream API was using Get verb, ocelot route used Post. I'm blind....
Please close this issue if resolved!
hi
@PaybackMan commented on Jul 27, 2022
Resolved by the author.
You must use "Routes"
instead of "ReRoutes"
in JSON!!!