Skip to content
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

HttpClient.PostAsJsonASync does not include a content-length header. #111659

Closed
nikolajwinnesacubiz opened this issue Jan 21, 2025 · 6 comments
Closed
Labels
area-System.Net.Http needs-author-action An issue or pull request that requires more info or actions from the author.

Comments

@nikolajwinnesacubiz
Copy link

nikolajwinnesacubiz commented Jan 21, 2025

Description

When using HttpClient.PostAsJsonAsync(value), the value of Content-Length is set to 0 (EDIT: As shown in reproduction steps, a Content-Length header is not present at all). This result in certain servers that receive the request fail to parse the request body.

The cause for this behaviour seem to be that JsonContent.TryComputeLength always return 0 as the length of the content, opposed to e.g. StringContent which return the length of the input.

Reproduction Steps

using System.Net.Http.Json;
using System.Text.Json;

var sut = new HttpClient();
var person = new { Name = "John Doe", Age = 33 };

//Content-Length included with StringContent
var json = JsonSerializer.Serialize(person);
var response1 = await sut.PostAsync("https://httpbin.org/post", new StringContent(json, new System.Net.Http.Headers.MediaTypeHeaderValue("application/json")));
Console.WriteLine($"Content-Length included in headers: {response1.RequestMessage?.Content?.Headers.Any(h => h.Key == "Content-Length")}"); //RETURNS TRUE

//Content-Length NOT included with JsonContent
var response2 = await sut.PostAsJsonAsync("https://httpbin.org/post", person);
Console.WriteLine($"Content-Length included in headers: {response2.RequestMessage?.Content?.Headers.Any(h => h.Key == "Content-Length")}"); // RETURNS FALSE

Expected behavior

Request sent to server include a content-length header with a value of the actual content length.

Actual behavior

Request sent to server include a content-length header with a value of 0.

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK 8.0.405
MacOS 15.2
ARM64

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jan 21, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

@stephentoub
Copy link
Member

the value of content-length is set to 0

It shouldn't be set to 0, it should be using chunked encoding. Can you share a trace of it including the Content-Length header set to 0?

It uses chunked encoding because it doesn't know the length the content will be when it starts to serialize it directly to the underlying stream. To know the length, it would need to buffer up the whole thing.

@MihaZupan
Copy link
Member

The cause for this behaviour seem to be that JsonContent.TryComputeLength always return 0 as the length of the content

Note the Try in the name. It's returning false, so the computed length is ignored. Because of that chunked encoding is used instead as Stephen mentioned.

@MihaZupan MihaZupan added the needs-author-action An issue or pull request that requires more info or actions from the author. label Jan 21, 2025
Copy link
Contributor

This issue has been marked needs-author-action and may be missing some important information.

@huoyaoyuan
Copy link
Member

huoyaoyuan commented Jan 21, 2025

Related to #49357

JsonContent uses chunked encoding by default. LoadIntoBufferAsync can force the serialization and fixed-length encoding.

There are servers in the wild don't support chunked request body. https://stackoverflow.com/questions/30924004/php-how-to-read-post-body-with-transfer-encoding-chunked-no-content-length https://stackoverflow.com/questions/54444220/php-7-fpm-does-not-show-request-body-with-chunked-encoding

@nikolajwinnesacubiz
Copy link
Author

Thank you for clarifying the behaviour. As my reproduction code also shows, there was no Content-Length header present in my request, and not as I stated in the initial report, that Content-Length had a value of 0.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Net.Http needs-author-action An issue or pull request that requires more info or actions from the author.
Projects
None yet
Development

No branches or pull requests

4 participants