Skip to content

Can't create a body in the transform if the original request doesn't contain one #2867

@riskeez

Description

@riskeez

Describe the bug

In v2.2.0 we had following code of custom transform

public override async Task ApplyRequestTransform(RequestTransformContext requestContext)
{   
   requestContext.ProxyRequest.Content = new StringContent(body, Encoding.UTF8, contentType);
}

After upgrade to v2.3.0, we can't use this code anymore due to new exception that is being thrown (See #2722).

I'm trying following logic to make it work, but no luck. Downstream service receives empty body.

var bodyBytes = Encoding.UTF8.GetBytes(body);
requestContext.HttpContext.Request.Body = new MemoryStream(bodyBytes);
requestContext.HttpContext.Request.Headers.ContentLength = bodyBytes.Length;

requestContext.ProxyRequest.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(contentType);

I am not able to find a workaround for this :(

To Reproduce

Just transform GET request to POST with body

  1. Make a request transform that converts GET request to POST setting new body.
  2. Downstream service will receive empty body.

Further technical details

  • Yarp 2.3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions