Skip to content

HttpRequest.Form throws an undocumented System.InvalidOperationException #49096

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

Open
1 task done
Grauenwolf opened this issue Jun 29, 2023 · 2 comments
Open
1 task done
Assignees
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions bug This issue describes a behavior which is not expected - a bug. Docs This issue tracks updating documentation help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team net8_docathon This issue is part of the documentation push in Fall 2023.

Comments

@Grauenwolf
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

After upgrading from ASP.NET Core 3.x to ASP.NET Core 6, we found that HttpRequest.Form throws an undocumented System.InvalidOperationException with the message "Incorrect Content-Type: image/png".

System.InvalidOperationException: Incorrect Content-Type: image/png
   at Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()
   at Microsoft.AspNetCore.Http.DefaultHttpRequest.get_Form()

This seems to only affect image files. Using the same code to read CSV files continues to work.

Expected Behavior

Form object is returned.

Steps To Reproduce

No response

Exceptions (if any)

System.InvalidOperationException

.NET Version

7.0.304

Anything else?

@ghost ghost added the area-runtime label Jun 29, 2023
@Grauenwolf
Copy link
Author

Looking at the code, there are two paths it can go down. One throws...

            throw new InvalidOperationException("This request does not have a Content-Type header. Forms are available from requests with bodies like POSTs and a form Content-Type of either application/x-www-form-urlencoded or multipart/form-data.");

the other throws...

throw new InvalidOperationException("Incorrect Content-Type: " + _request.ContentType);

~\src\Http\Http\src\Features\FormFeature.cs

Neither is complete, as really we want both the current request type and the desired request type.

@Tratcher
Copy link
Member

Tratcher commented Jun 30, 2023

Code links:

if (!HasFormContentType)
{
throw new InvalidOperationException("This request does not have a Content-Type header. Forms are available from requests with bodies like POSTs and a form Content-Type of either application/x-www-form-urlencoded or multipart/form-data.");

if (!HasFormContentType)
{
throw new InvalidOperationException("Incorrect Content-Type: " + _request.ContentType);

Yes, both code paths check the same condition and should return the same exception type and message. And yes we should add this exception to the doc comments, as well as OperationCancelledException (ReadFormAsync), InvalidDataException, and IOException

@Tratcher Tratcher added bug This issue describes a behavior which is not expected - a bug. help wanted Up for grabs. We would accept a PR to help resolve this issue Docs This issue tracks updating documentation labels Jun 30, 2023
@adityamandaleeka adityamandaleeka added the net8_docathon This issue is part of the documentation push in Fall 2023. label Aug 25, 2023
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
@mkArtakMSFT mkArtakMSFT added help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team and removed help wanted Up for grabs. We would accept a PR to help resolve this issue labels Oct 28, 2023
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 6, 2024
@wtgodbe wtgodbe removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
@dotnet dotnet deleted a comment from dotnet-policy-service bot Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions bug This issue describes a behavior which is not expected - a bug. Docs This issue tracks updating documentation help candidate Indicates that the issues may be a good fit for community to help with. Requires work from eng. team net8_docathon This issue is part of the documentation push in Fall 2023.
Projects
None yet
Development

No branches or pull requests

7 participants