reverse-proxy icon indicating copy to clipboard operation
reverse-proxy copied to clipboard

Using latest .NET 9 with Yarp Nuget package -> Adding Headers to the ProxyRequest does not work anymore. The destination API does not receive the added Request Headers, but has still the original headers from the start (like Postman Call).

Open pastrasigns opened this issue 1 year ago • 5 comments

Using latest .NET 9 with Yarp Nuget package -> Adding Headers to the ProxyRequest does not work anymore. The destination API does not receive the added Request Headers. It is working when using Target Frameowkr .NET 8 latest for both the WEB API Yarp Gateway and the ASP.NET Web API out of the box Weather endpoint.

To Reproduce

  1. Add some Transforms like so:
builder.Services.AddReverseProxy()
    .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
    .AddTransforms((Action<TransformBuilderContext>)(tbc =>
    {
        tbc.AddRequestTransform(rtc =>
        {
            rtc.ProxyRequest.Headers.Add("userid", "123123");

            rtc.ProxyRequest.Headers.Add("CustomHeader", "CustomValue");

etc....

Got Exceptions? Include both the message and the stack trace

  • No exceptions are thrown.

Further technical details

  • Latest Nuget package for Yarp.ReverserProxy => Version 2.2.0
  • Windows 11 -> VS 2022 latest.
  • .NET 9 latest Target Framwork for both the ASP.NET Yarp API, and the other .NET 9 latest "Weather API" (for quick testing)
  • Use latest Default POSTMAN to call YARP endpoint.

pastrasigns avatar Feb 04 '25 20:02 pastrasigns

@MihaZupan

Hey a created a simple SOlution in my Repo for you to just run and check why this isn't working:

https://github.com/pastrasigns/YarpGateway9

  1. Download/Update to latest Docker Desktop (Default settings)
  2. I guess you may use Postman and HttpGet to URL: https://localhost:5001/gateway/api/weatherforecast
  3. Place breakpoint here:

Image

  1. Place breakpoint within the gateway here:

Image

  1. You'll notice the "MyNewHeader2" request header value doesn't show up in the weather httpget.

Is this a bug?

Image

pastrasigns avatar Feb 06 '25 21:02 pastrasigns

@MihaZupan

Also; I noticed I'm not able to change the Address here to the container name like this "Address": "http://weatherapi:5100/" (this doesn't work anymore when I'm using .net 9 with the latest Desktop Docker for Windows 11 Pro.

Image

pastrasigns avatar Feb 06 '25 21:02 pastrasigns

@MihaZupan

I also have a .NET 8 version repo setup for you and your team if needed, same Solution but .Net8

https://github.com/pastrasigns/YarpGateway8

pastrasigns avatar Feb 06 '25 22:02 pastrasigns

Please refrain from pinging individuals on every comment unless the question is specific to them. The relevant people have notifications enabled and will respond when appropriate.


You have HTTPS redirection configured on the backend application https://github.com/pastrasigns/YarpGateway9/blob/2a037f30e4ce46134d2187e779adb66af7b574ff/WeatherAPI9/Program.cs#L33 but you configured YARP to talk to the http endpoint https://github.com/pastrasigns/YarpGateway9/blob/2a037f30e4ce46134d2187e779adb66af7b574ff/YarpGateway9/appsettings.Development.json#L25

You're not seeing the header being added because your requests aren't going through the proxy. The request gets redirected to the https variant of the backend service, bypassing the proxy. This may also explain why you're having issues with name resolution.

For future reference please look at the logs emitted by the proxy. You would immediately see that a request resulted in a redirect response, and that no future requests are hiting the proxy.

MihaZupan avatar Feb 07 '25 00:02 MihaZupan

@MihaZupan Thanks this resolved the issue(s) at hand. I hope someone else can one day see this if they missed the HTTPS app.UseHttpsRedirection();

Thanks. Resolved.

pastrasigns avatar Feb 07 '25 17:02 pastrasigns