Closed inforly closed 1 month ago
I actually expected your code to work as-is.
The way we're detecting the presence of a request body isn't accounting for the body being created for a request that originally had none. You can "fix" that with this if you do so:
context.Request.Body = new MemoryStream(queryBytes);
+context.Features.Set<IHttpRequestBodyDetectionFeature>(null);
@Tratcher does that seem like something we'd want to change in ASP.NET (IHttpRequestBodyDetectionFeature.CanHaveBody
)?
As-is, this case hits a debug assert in YARP here: https://github.com/microsoft/reverse-proxy/blob/181efd33d77c7ffe07a096aa682fc8d0e8239b99/src/ReverseProxy/Forwarder/RequestUtilities.cs#L326-L331
I actually expected your code to work as-is.
The way we're detecting the presence of a request body isn't accounting for the body being created for a request that originally had none. You can "fix" that with this if you do so:
context.Request.Body = new MemoryStream(queryBytes); +context.Features.Set<IHttpRequestBodyDetectionFeature>(null);
@Tratcher does that seem like something we'd want to change in ASP.NET (
IHttpRequestBodyDetectionFeature.CanHaveBody
)?As-is, this case hits a debug assert in YARP here:
It works, thanks a lot!
Changing a GET to a POST is an unusual transformation. Rather than dropping IHttpRequestBodyDetectionFeature I'd suggest replacing it with one that returns true.
@inforly, you'll also want to remove that parameter from the query to avoid duplication.
Glad this is working for you now @inforly. Since this is the only issue we've seen about this (and as @tratcher pointed out, it's an unusual transformation), I'm closing this issue now.
Changing a GET to a POST is an unusual transformation. Rather than dropping IHttpRequestBodyDetectionFeature I'd suggest replacing it with one that returns true.
@inforly, you'll also want to remove that parameter from the query to avoid duplication.
@Tratcher could you please give the details for this?
@inforly something like:
class MyBodyDetectionFeature : IHttpBodyDetectionFeature {
CanHaveBody => true;
}
context.Features.Set<IHttpRequestBodyDetectionFeature>(new MyBodyDetectionFeature());
Describe the bug
We are developing a middleware to change the request (transform is not suitable here, since we want to the change happens in the 1st middleware): if a GET request has the q query parameter, the middleware will change it to a POST request and set the value of the q query parameter as the body of the request, the code like:
But when we send the request like:
curl http://localhost:5000/?q=test
, it will throw the error:Could you please take a look of this, how to change the body correctly for such scenario?
To Reproduce
Further technical details