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

Provide better json support for servers don't support chunked request body #49357

Closed
huoyaoyuan opened this issue Mar 9, 2021 · 5 comments
Closed

Comments

@huoyaoyuan
Copy link
Member

Background

JsonContent and PostAsJsonAsync provided good support for calling json web apis. To save buffer usage, it serializes directly into send buffer, which doesn't calculate content length before serialization, and uses chunked encoding.

However, there are certain amount of web servers don't support chunked encoding in request body, namely PHP. They would see chunked encoding as empty request. Discovering such issue is not straight.

The current workaround is to use LoadIntoBufferAsync:

var content = JsonContent.Create<T>(json);
await content.LoadIntoBufferAsync();
var response = await httpClient.PostAsync(url, content);

This is hard to discover, and prevents the usage of PostAsJsonAsync.

Possible solutions

  • Add new overloads to JsonContent.Create and PostAsJsonAsync, with a parameter indicating whether to use chunked encoding.
    This can add a large amount of overloads.
  • Serialize into a buffer eagerly
    Can downgrade performance for servers do support chunked request
  • Change the http pipeline to calculate content length before sending
    The change is too broad
@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Net.Http untriaged New issue has not been triaged by the area owner labels Mar 9, 2021
@ghost
Copy link

ghost commented Mar 9, 2021

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

Issue Details

Background

JsonContent and PostAsJsonAsync provided good support for calling json web apis. To save buffer usage, it serializes directly into send buffer, which doesn't calculate content length before serialization, and uses chunked encoding.

However, there are certain amount of web servers don't support chunked encoding in request body, namely PHP. They would see chunked encoding as empty request. Discovering such issue is not straight.

The current workaround is to use LoadIntoBufferAsync:

var content = JsonContent.Create<T>(json);
await content.LoadIntoBufferAsync();
var response = await httpClient.PostAsync(url, content);

This is hard to discover, and prevents the usage of PostAsJsonAsync.

Possible solutions

  • Add new overloads to JsonContent.Create and PostAsJsonAsync, with a parameter indicating whether to use chunked encoding.
    This can add a large amount of overloads.
  • Serialize into a buffer eagerly
    Can downgrade performance for servers do support chunked request
  • Change the http pipeline to calculate content length before sending
    The change is too broad
Author: huoyaoyuan
Assignees: -
Labels:

area-System.Net.Http, untriaged

Milestone: -

@scalablecory
Copy link
Contributor

Are you sure this is not some PHP (or Apache/nginx etc.) configuration that you're missing? It seems very weird that it would straight up not support chunked encoding.

If you are sure, has an issue been filed with PHP? It lacking support for a major part of the HTTP/1.1 standard is very strange and should be looked at.

I'm generally against adding workarounds when we're spec compliant. But, PHP being a large platform does make this more compelling. If we can verify that this is a limitation there, it might make sense to add an API to always buffer request content.

@scalablecory scalablecory added needs more info and removed untriaged New issue has not been triaged by the area owner labels Mar 9, 2021
@scalablecory scalablecory added this to the 6.0.0 milestone Mar 9, 2021
@karelz
Copy link
Member

karelz commented Apr 23, 2021

I agree that compensating for missing HTTP/1.1 standard implementation feels weird.
Given there is no detail about wide-spread server usage with chunked encoding disabled / not working, we should close this until more evidence appears.

@karelz karelz closed this as completed Apr 23, 2021
@davidfowl
Copy link
Member

Can we improve the docs to show the above and not recommend buffering for large JSON payloads

@huoyaoyuan
Copy link
Member Author

huoyaoyuan commented Apr 23, 2021

Are you sure this is not some PHP (or Apache/nginx etc.) configuration that you're missing?

The answer is Yes, but it not I'm missing. The developers of certain popular sites/rpc peers misses it.

Search for "php chunked request", the first issue from StackOverflow indicates that it needs user-side explicit handling in PHP.
I've also mentioned that aria2 doesn't support chunked request.

Given that the popular browser doesn't use it, and popular tools like curl may not be using it, it's hard to say that this HTTP standard is widely obeyed.

@ghost ghost locked as resolved and limited conversation to collaborators May 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants