dotnet / dotnet/yarp

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

Open
#2,867 3 comments 0 reactions 0 assignees View on GitHub
Type: Bug
Dominant language
C#
Stars
9.6k
Forks
933
Avg merge
12d 18h
Merged PRs (30d)
2

Description

### Describe the bug

In v2.2.0 we had following code of custom transform
```csharp
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.
```csharp
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.
3. Downstream service will receive empty body.

### Further technical details
- Yarp 2.3.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.