-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Why does HttpClient in Core allow GET requests with bodies, while Framework version does not? #25485
Comments
This maybe a breaking change with older versions of HttpClient, but it is useful. |
It would be good to know if SocketsHttpHandler allows it or not (it should for compat with curl/winhttp). If it does not, please file a new bug. .NET Framework is separate (older) implementation based on Sockets as well. If it doesn't allow these request, it is likely a more strict RFC interpretation. Given that GET bodies are useless (they are ignored by servers) and that it is not a regression on .NET Framework, I don't see any reason to change .NET Framework -- there is easy workaround and the scenario is very corner-case. Closing as question answered. If you disagree feel free to reopen / ping me. Thanks! |
It does. |
From RFC7231:
So, either way is ok. Servers might reject a GET request with a body. The design picked in .NET Core was to be flexible and allow this since the RFC doesn't explicitly forbid it. |
Would you mind expanding on the "easy workaround"? I need to implement an API which requires GET requests with a body (idealy using a ITypeHttpClientFactory). The particular API is deprecating all GET request w/out body support. Thanks. |
This particular issue was about questions regarding why .NET Core behavior is different from .NET Framework. That question was answered and this issue closed.
In terms of the "easy workaround" that @karelz mentioned, I don't think one exists. The only possibility is to use a different handler with the HttpClient API. For example, WinHttpHandler is available as a separate NuGet package (System.Net.Http.WinHttpHandler) for .NET Framework. If someone used that underneath HttpClient, then they could probably be able to use GET requests with a request body. |
Thanks davidsh. That worked. services.AddHttpClient<ApiService>()
.ConfigureHttpMessageHandlerBuilder(c => {
c.PrimaryHandler = new WinHttpHandler(); //Windows only. Required to send body with GET requests.
}) |
I am using WinHttpHandler but now im loosing UseDefaultCredentials = true that i had with HttpClientHandler, and now i have too use NetworkCredential is there a way too use WinHttpHandler with UseDefaultCredentials |
HttpClientHandler.UseDefaultCredentials is really a duplicate property. You can use HttpClientHandler.Credentials property instead. Just set HttpClientHandler.Credentials = CredentialCache.DefaultCredentials and it is the same thing as setting UseDefaultCredentials=true. So, for WinHttpHandler, just set the WinHttpHandler.ServerCredentials property to CredentialCache.DefaultCredentials. Also, in the future, please don't comment on closed issues. They are rarely monitored. If you have a question, please open a new issue. Thanks. |
Thanks for the help its working now with WinHttpHandler |
Opened on behalf of @IanKemp from dotnet/core#1333
@IanKemp writes -
As per the title, the
HttpClient
implementation between Core and Framework differs in this regard. Consider the following example:ProtocolViolationException
with the messageCannot send a content-body with this verb-type
on theSendAsync
call.While I am very happy that Core allows this (technically correct, but unusual) request type, I am less happy that the Framework does not support it. Why does Core allow this why Framework does not? Is this intentional or an oversight? Is there somewhere where these differences/idiosyncrasies are documented?
(For another example of differing HTTP behaviour in Core vs Framework, see this issue.)
@AppBeat writes -
Interesting. According to this post: https://stackoverflow.com/questions/978061/http-get-with-request-body
standard does not explicitly forbid this. GET body should be ignored by server.
@IanKemp writes -
@AppBeat The server is free to ignore or accept the body; that should not prevent the client from sending a request that the server may ignore. It is ultimately up to the client to determine whether the server accepts requests like this.
A popular example of a product (server) that supports GET requests with bodies is Elasticsearch, specifically their REST query API. In particular, the section on that page "A GET Request with a Body?" explains their rationale (but note that they do also allow POST as a fallback option for clients that do not support this).
@AppBeat writes -
I didn't say that this should be removed from .NET Core :) Although this is not common (bad?) practice I think .NET Core version in this case is more correct than .NET Framework implementation.
I will try to test this on new managed implementation of SocketsHttpHandler which will probably be prefered HttpHandler in future for more consistent behaviour across all different platforms.
https://github.com/dotnet/corefx/tree/master/src/System.Net.Http/src/System/Net/Http/SocketsHttpHandler
I created new functional test for SocketsHttpHandler and it works as it should:
The text was updated successfully, but these errors were encountered: