Search code examples
c#httpasp.net-coregatewayocelot

Ocelot - Placeholder different in Downstream-Upstream


I have an API Gateway (C#, net.Core 3.1, Ocelot), everything is working fine but now I'm trying to configure different routes for upstream and downstream, because my Gateway retrieve information during the process and send this information to the final API.

In upstream I don't have a placeholder {userid}, but I want to have it in downstream.

Here is my ocelot.json:

  "DownstreamPathTemplate": "/api/User/{userid}",
  "DownstreamScheme": "http",
  "DownstreamHostAndPorts": [
    {
      "Host": "localhost",
      "Port": 44301
    }
  ],
  "UpstreamPathTemplate": "/api/User/",
  "UpstreamHttpMethod": [ "Get" ],

And there is how I'm adding in the middleware the placeholder value:

   if (context.DownstreamRequest.OriginalString.Contains("User"))
   {
       context.DownstreamRequest.AbsolutePath = 
       context.DownstreamRequest.AbsolutePath + userid; //this variable is valued before
   }

So, to be more clear, here is an example:

I get called at http://localhost:44358/api/User/ (mygateway Upstream), from some logics I get the userid that made this call, for example Andrew, and I want to redirect the request to my API http://localhost:44301/api/User/Andrew (mygateway Downstream).

Everything is fine, except at my API the userid is coming as {userid} and not has userid value (Andrew).


Solution

  • You can implement using Claims Transformation feature of Ocelot.

    e.g.

    "AddQueriesToRequest": {
    "LocationId": "Claims[LocationId] > value",
    }