-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
System.Net.Http.Json: Consider relaxing content type verification in ReadFromJsonAsync #38713
Comments
Tagging subscribers to this area: @dotnet/ncl |
Yeah. There are so many weird servers that send weird content media types, in which case The first option would solve all problems:
|
This seems worth doing to me. At least to make it configurable. |
Why would this need to be configurable? Couldn't you just check the Content-Type header yourself before calling |
Do we enforce content type for other methods like ReadAsStringAsync? Seems like we just shouldn't check Content-Type here at all. |
Yep, I should have been more clear -- I think we should get rid of the check. And if there is a reason to care (@rynowak? @jozkee?) then it should be configurable. |
The main reason to have it was to have parity with the old APIs from Linking the original discussion for such check #33459 (comment). If other Keep in mind that these APIs are already available as a NuGet package that targets netcoreapp3.1 and netstandard2.0; if we decide to change behavior here would that be considered a breaking change? |
System.Net.Http.Formatting performed content-negotation, hence it made sense for it to complain about an unknown media type. With S.Net.Http.Json since you're explicitly asking to read JSON, there isn't much value in trying to validate this. What would help, is to include the media type as part of the error if deserializing fails and the content-type is unknown. This may help users self-diagnose a little better: try
{
return DeserializeAsync();
}
catch (JsonException ex) when (IsNotJsonContentType())
{
throw new DivideByZeroException("It does not look like you're reading json. Here's your content-type {content-type}. ", ex);
}
The code would have previously thrown when it encountered an unsupported media type. It wouldn't be a breaking change if it was more accomodating. |
The original intent for the check (in this iteration of the library) is to make sure you get a reasonable error message when parsing fails. The really common case is when you're interacting with a website that returns an HTML page on a failure. |
So what do you think the ideal change is? We could check the content type and throw a better exception for known content types that are common and unlikely to produce JSON, such as |
I think we should not check content type, but if parser throws an exception, throw with a message like "Content of type '{0}' was not valid json...". |
👍 with @scalablecory's suggestion. We want to give users good diagnostics when parsing fails rather than police the web. |
In that case this is my understanding of the change:
|
I'm doing interop with aria2. It's using
application/json-rpc
for its returning content type, which is refused byReadFromJsonAsync
.Proposed solutions:
StartsWith
is abusing it.Current workaround:
Copy the implementation and delete code doing verification.
This is opposed to the purpose of
System.Net.Http.Json
. They are mostly simple helpers. If there's some restriction makes it unusable, the whole type becomes no-sense.The text was updated successfully, but these errors were encountered: