Chris
Chris

Reputation: 379

Can yarp transform a request with information from the request header?

I am trying to make a reverse proxy in my application. In my case, I need the path to be transformed to include information stored in the request header.

"ReverseProxy": {
"Routes": {
  "MyRoute": {
    "ClusterId": "MyCluster",
    "AuthorizationPolicy": "DefaultPolicy",
    "Match": {
      "Path": "/api/{**remainder}"
    },
    "Transforms": [
      { "PathPattern": "/api/{item}/{**remainder}" },
      {
        "ResponseHeader": "Source",
        "Append": "YARP",
        "When": "Success"
      }
    ]
  }
},
"Clusters": {
  "MyCluster": {
    "Destinations": {
      "MyCluster/destination": {
        "Address": "https://myAddress.com/"
      }
    }
  }
}

Trying to create a custom transform:

services.AddReverseProxy()
.LoadFromConfig(_configuration.GetSection("ReverseProxy"))
.AddTransforms(builderContext =>
{
    builderContext.RequestTransforms.Add(new Yarp.ReverseProxy.Transforms.RequestTransform()
    {

    }
});

Can I replace {item} with the information included in the request header?

Upvotes: 1

Views: 8152

Answers (1)

Chris
Chris

Reputation: 379

You can create a proxy pipeline middleware.

app.UseEndpoints(endpoints =>    
{
    endpoints.MapReverseProxy(proxyPipeline =>        
    {
        proxyPipeline.Use((context, next) =>
        {
            //You can use context to change request before they are sent.
        });
    });
});

Follow this guide on adding middleware. The placement of UseRouting and UseEndpoints matters when creating middleware for authentication.

Upvotes: 3

Related Questions