Skip to content

Request body transforms only allowed on non-empty requests already #2809

@vtali

Description

@vtali

Describe the bug

Per documentation we are able to modify the body.
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/yarp/extensibility-transforms?view=aspnetcore-9.0#request-body-transforms

This only true if the request already comes in with a body.

I have a requirement where I need to on a specific POST route, add a default body.
The POST request will come from a source that I'm not able to modify, but need to then, add a body before sending it to its downstream destination.

To Reproduce

Submitting a POST with no body results in the: Replacing the YARP outgoing request HttpContent is not supported.
This works if I do have a body of course.

context.AddRequestTransform(async requestContext =>
                {
                    using var reader = new StreamReader(requestContext.HttpContext.Request.Body);
                    // TODO: size limits, timeouts
                    var body = await reader.ReadToEndAsync();
                    //if (!string.IsNullOrEmpty(body))
                    {
                        body = "test=test";
                        var bytes = Encoding.UTF8.GetBytes(body);
                        // Change Content-Length to match the modified body, or remove it.
                        requestContext.HttpContext.Request.Body = new MemoryStream(bytes);
                    }
                });

Is there somewhere else in the pipeline that I can add a body to the request in a YARP way?
I can do this in the middleware before Yarp, but then it feels like overkill for a single route to take over?
Thank you for any suggestions/advice

PS. Reporting it as bug either documentation (to describe this only applicable to requests that already come in with a body) or if it's not intended to be stopped like it is right now?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: DocumentationImprovements or additions to documentationhelp wantedWe will welcome a contribution

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions