Skip to content

EmptyBodyBehavior.Allow should allow a missing Content-Type #36466

Closed
@BieleckiLtd

Description

@BieleckiLtd

EmptyBodyBehavior.Allow allows a 0-length body, but does not work if the request does not have a Content-Type header. To make bodies truly optional, we need to expand the behavior of the switch to allow a missing Content-Type.

Original bug:

Describe the bug

This is either a bug or I'm using this new ASP.NET Core 5.0 feature wrong/API doc is not clear.

To Reproduce

  1. Create new ASP.NET Core Web API from the default template, VS2019, target Framework = .NET 5.0 (Current)

  2. Add below action at the end of the existing WeatherForecastController.cs:

[HttpPost("[action]")]
public async Task<IActionResult> MyAction([FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] IEnumerable<int> elements = default)
{
    return Ok(elements);
}
  1. I am using Postman, my application is available at port 44349.
  2. I set Body as 'raw', body type JSON and body: [1,2]
  3. I send POST request to https://localhost:44349/WeatherForecast/MyAction
  4. SUCCESS, response is 200 body is [1,2]
  5. I clear request body (still 'raw', JSON)
  6. FAILURE, response is 415 Unsupported Media Type. I would expect 204 No Content
  7. I change Body as 'none'
  8. FAILURE, response is 415 Unsupported Media Type. I would expect 204 No Content

Exceptions (if any)

No exceptions, action is not being hit in point 8 and 10

Further technical details

  • ASP.NET Core version 5.0
  • dotnet --info
    .NET SDK (reflecting any global.json):
    Version: 5.0.301
    Commit: ef17233f86

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.301\

Host (useful for support):
Version: 5.0.7
Commit: 556582d964

.NET SDKs installed:
2.1.602 [C:\Program Files\dotnet\sdk]
2.1.700 [C:\Program Files\dotnet\sdk]
2.1.801 [C:\Program Files\dotnet\sdk]
2.2.401 [C:\Program Files\dotnet\sdk]
5.0.301 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.28 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

  • The IDE (VS / VS Code/ VS4Mac) VS2019 Pro (16.10.3)

Related

#30690

Metadata

Metadata

Assignees

Labels

bugThis issue describes a behavior which is not expected - a bug.feature-model-bindingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions