-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
HttpRequest.ContentLength is null when using TestHost #18463
Comments
I think this would depend on what |
Sorry I've been absent, been dealing with SameSite cookie fun and haven't had a chance to think about this and then am on hols until Thursday. How could I explicitly set the content length? I notice that if I do Will have more of a think about this next week. |
Hrm, that is interesting. There may be a bug for us to look at here. We'll see what we find. I'd definitely agree that if the content has a
Oh we're certainly familiar with that ;). No worries! |
@Tratcher was saying that the Still, I think we can do something to fix this. It seems like there's some weird edge case stuff going on here that we can try to tidy up. |
Could I maybe take a whack at this if it's still available? Is the idea to set the header |
Yeah, when copying request headers from HttpContent the Content-Length header needs to be checked separately. Give it a try. |
I'm running into this same issue, which unfortunately causes ServiceStack framework to not hydrate request dto's. In case it's useful to anyone else, I managed to work around the issue by doing the following on the WebHostBuilder:
It's not ideal but works to get the content length set, which gets everything else working properly downstream. |
@TheDruidsKeeper that assumes the entire request body is read in one read. A better workaround would be to set the content length on the client. |
@Tratcher Apparently I had gone way off the deep end... I had found that on the client side the StringContent.Headers.ContentLength did actually have a value, so spent all my time looking server side. I did a bit more testing now and found that the easiest solution is to simply read the ContentLength property:
This solution works perfectly (and is much better than my previous suggestion). |
@TheDruidsKeeper yeah, that's a known annoyance with HttpContent dotnet/runtime#16162. I contributed to that design flaw about 10 years ago so it's only fair that it comes back to haunt me now. |
@TheDruidsKeeper Confused me too ;) Sorry my comment wasn't more obvious. Perhaps should add to the desc. |
Same issue here, I'm going to add a couple keywords to this issue hoping that anyone Googling the problem will find this issue (and workaround) quicker than I did. Don't mean to spam this but just spent 5 hours searching the wrong terms until I realized it was related to the content length which then brought me directly to here. HttpClient post to TestServer is missing body Workaround: private HttpContent CreateJsonContent(object obj)
{
var json = SerializeJson(obj);
var content = new StringContent(json, Encoding.UTF8, "application/json");
#pragma warning disable IDE0059 // Unnecessary assignment of a value
// required due to https://github.com/dotnet/aspnetcore/issues/18463
var contentLenth = content.Headers.ContentLength;
#pragma warning restore IDE0059 // Unnecessary assignment of a value
return content;
} |
Possibly a better workaround, since I needed this elsewhere, I've moved it to a new class under my testing namespace. public class StringContentWithLength : StringContent
{
public StringContentWithLength(string content)
: base(content)
{
EnsureContentLength();
}
public StringContentWithLength(string content, Encoding encoding)
: base(content, encoding)
{
EnsureContentLength();
}
public StringContentWithLength(string content, Encoding encoding, string mediaType)
: base(content, encoding, mediaType)
{
EnsureContentLength();
}
public StringContentWithLength(string content, string unvalidatedContentType)
: base(content)
{
Headers.TryAddWithoutValidation("Content-Type", unvalidatedContentType);
EnsureContentLength();
}
private void EnsureContentLength()
{
#pragma warning disable IDE0059 // Unnecessary assignment of a value
// required due to https://github.com/dotnet/aspnetcore/issues/18463
var contentLenth = Headers.ContentLength;
#pragma warning restore IDE0059 // Unnecessary assignment of a value
}
} |
Describe the bug
Apologies if I'm completely wrong here.
Over hereI'm upgrading a middleware to 3.x andSystem.Text.Json
.I'm trying to optimize a deserialization from the
HttpRequest.Body
by pre-allocating a buffer using the shared array pool givenHttpRequest.ContentLength
.See here.Problem is, when our integration tests run, which useHttpClient.PostAsync(..., new StringContent(...))
to post to a TestHost, theContent-Length
header is always null.Is this is a limitation of the test host, or a bug? I guess I could write a unit test the specific internals of that function.
To Reproduce
PRbranch linked above.GraphQLHttpMiddleware
in the JsonContentType case of the switch statement.httpRequest.ContentLength
will be null.Further technical details
dotnet --info
The text was updated successfully, but these errors were encountered: