Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access the response body #2701

Closed
beukerz opened this issue Jan 2, 2025 · 5 comments
Closed

Unable to access the response body #2701

beukerz opened this issue Jan 2, 2025 · 5 comments
Labels
needs-author-action An issue or pull request that requires more info or actions from the author. Type: Bug Something isn't working

Comments

@beukerz
Copy link

beukerz commented Jan 2, 2025

Describe the bug

In middleware or in a transform the response body is always empty.

To Reproduce

Transform code:

builder.Services.AddReverseProxy()
            .LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"))
            .AddTransforms(context =>
            {
                // stole from https://microsoft.github.io/reverse-proxy/articles/transforms.html#response-body-transforms
                context.AddResponseTransform(async responseContext =>
                {
                    if (responseContext.ProxyResponse != null)
                    {
                        var stream = await responseContext.ProxyResponse.Content.ReadAsStreamAsync();
                        using var reader = new StreamReader(stream);
                        var body = await reader.ReadToEndAsync();
                        Console.WriteLine("Response body: " + body);
                    }
                });
            });

Middleware code:

app.MapReverseProxy(proxyPipeline =>
{
    proxyPipeline.Use(async (httpContext, next) =>
    {
        try
        {
            httpContext.Request.EnableBuffering();
            string requestBody = await new StreamReader(httpContext.Request.Body, Encoding.UTF8).ReadToEndAsync();
            httpContext.Request.Body.Position = 0;
            Console.WriteLine($"Request body: {requestBody}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Exception reading request: {ex.Message}");
        }

        Stream originalBody = httpContext.Response.Body;
        try
        {
            using var memStream = new MemoryStream();
            httpContext.Response.Body = memStream;

            await next(httpContext);

            memStream.Position = 0;
            string responseBody = new StreamReader(memStream).ReadToEnd();

            memStream.Position = 0;
            await memStream.CopyToAsync(originalBody);
            Console.WriteLine("Response body: " + responseBody);
        }
        finally
        {
            httpContext.Response.Body = originalBody;
        }
    });
    proxyPipeline.UseSessionAffinity();
    proxyPipeline.UseLoadBalancing();
    proxyPipeline.UsePassiveHealthChecks();
});

In both situations the body is always an empty string.

Further technical details

  • Include the version of the packages you are using
    2.2.0
  • The platform (Linux/macOS/Windows)
    Windows

Sample repository

https://github.com/[beukerz/yarpsample](https://github.com/beukerz/yarpsample)

@beukerz beukerz added the Type: Bug Something isn't working label Jan 2, 2025
@MihaZupan
Copy link
Member

Can you create a minimal runnable example where it doesn't work in your case? The sample code does "work on my machine"™.
What kind of requests are you testing it with?

Note that as-written, the response transform would break the request as the content can't be re-read, but it should still print the body to the console before doing so.

@MihaZupan MihaZupan added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 2, 2025
@beukerz
Copy link
Author

beukerz commented Jan 2, 2025

Can you create a minimal runnable example where it doesn't work in your case? The sample code does "work on my machine"™. What kind of requests are you testing it with?

Note that as-written, the response transform would break the request as the content can't be re-read, but it should still print the body to the console before doing so.

I have added the link to a sample repo (https://github.com/[beukerz/yarpsample](https://github.com/beukerz/yarpsample))

I am sending a simple get request to the weather api.

@MihaZupan
Copy link
Member

Do you have a step-by-step for what you've tried that doesn't work?
The route in the sample doesn't match the sample application, but assuming you fix that, it also does work as expected for me.
Is running it through containers necessary to reproduce the issue?

@beukerz
Copy link
Author

beukerz commented Jan 3, 2025

I just tested running the project without containers and it works. So it seems the issue only exists in containers.

@MihaZupan
Copy link
Member

Sounds like a non-YARP issue then. Let me know if we've missed something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-author-action An issue or pull request that requires more info or actions from the author. Type: Bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants