Skip to content

Web API XML POST requests working in 2.1 but not in 3.1 #24462

Closed
@cmhernandezdel

Description

@cmhernandezdel

Describe the bug

Web API POST requests sending a XML file over HTTP are working in ASP Net Core 2.1 project, but not in ASP Net Core 3.1 project with the same code, with the latter throwing a Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: "Unexpected end of request content."

To Reproduce

This is happening with Hikvision security cameras, so I am not sure about the source, but it should be reproducible with any application that sends "application/xml" bodies over HTTP.

  1. Download the source code at https://github.com/cmhernandezdel/TestEndpointSafire
  2. Run the TestEndpointAspNetCore2 project.
  3. Post a XML (application/xml content type) file to http://:7050. It is encouraged to POST from another origin, since this is how the bug happened in the first place (I cannot reproduce it with Postman).
  4. You should see an output that looks similar to this in console:
<?xml version="1.0" encoding="UTF-8"?>
<EventNotificationAlert version="1.0" xmlns="urn:psialliance-org">
<protocolType>HTTP</protocolType>
<channelID>1</channelID>
<dateTime>2020-07-31T10:29:38+01:00</dateTime>
<activePostCount>1</activePostCount>
<eventType>PeopleCounting</eventType>
<eventState>active</eventState>
<eventDescription>peopleCounting alarm</eventDescription>
<channelName>Camera 01</channelName>
<peopleCounting>
<statisticalMethods>realTime</statisticalMethods>
<RealTime>
<time>2020-07-31T10:29:38+01:00</time>
</RealTime>
<enter>1365</enter>
<exit>1329</exit>
<regionsID>1</regionsID>
</peopleCounting>
<childCounting>
<enter>798</enter>
<exit>791</exit>
</childCounting>
</EventNotificationAlert>
  1. Kill the TestEndpointAspNetCore2 project.
  2. Run the TestEndpointAspNetCore3 project.
  3. Post a XML (application/xml content type) file to http://:7050. It is encouraged to POST from another origin, since this is how the bug happened in the first place (I cannot reproduce it with Postman).
  4. You should see an exception, with an output that looks similar to this in console:
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unexpected end of request content.                       at Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException.Throw(RequestRejectionReason reason)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.ReadAsyncInternal(CancellationToken cancellationToken)
   at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
   at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)                                          at System.IO.StreamReader.ReadToEndAsyncInternal()
   at TestEndpointAspNetCore3.Controllers.SafireController.PostSafire() in C:\Users\chernandez\Desktop\TestEndpointAspNetCore3\TestEndpointAspNetCore3\Controllers\SafireController.cs:line 21
   at lambda_method(Closure , Object )
   at Microsoft.Extensions.Internal.ObjectMethodExecutorAwaitable.Awaiter.GetResult()                                      at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Exceptions (if any)

Microsoft.AspNetCore.Server.Kestrel.Core.BadHttpRequestException: Unexpected end of request content.

Further technical details

  • This is happening in ASP Net Core 3.1.
  • The error was found in Visual Studio Community 2019, version 16.4.5, in Windows 10.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions