I'm trying to send a POST request with body from a frontend client to an ASP.NET (.NET 7.0) server (code below).
const URL = API_LINK + `/task-types`
await axios({
method: 'POST',
url: URL,
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: `Bearer ${this.token}`,
},
data: JSON.stringify({
name: task.name,
description: task.description,
deactivated: task.deactivated,
}),
})
.then(res => console.log(res))
.catch(err => console.log(err))
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateTaskTypeCommand command)
{
await _mediator.Send(command);
return Ok();
}
On the backend side, it throws a ConnectionResetException
. I don't know how to solve this and I haven't found a suitable solution to this problem on the internet.
Below is information from the log:
2023-07-25 16:21:39.5054|0|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request starting HTTP/2 POST https://localhost:44344/api/task-types application/json 70
2023-07-25 16:21:39.5054|0|TRACE|Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware|All hosts are allowed.
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Routing.Matching.DfaMatcher|1 candidate(s) found for the request path '/api/task-types'
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Routing.Matching.DfaMatcher|Endpoint 'TimeboxTA.WebAPI.Controllers.TaskTypeController.Create (TimeboxTA.WebAPI)' with route pattern 'api/task-types' is valid for the request path '/api/task-types'
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Server.IIS.Core.IISHttpServer|Connection ID "17870283352276205781" disconnecting.
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware|Request matched endpoint 'TimeboxTA.WebAPI.Controllers.TaskTypeController.Create (TimeboxTA.WebAPI)'
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware|The request cannot be served from cache because it uses the HTTP method: POST.
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.Cors.Infrastructure.CorsService|The request has an origin header: 'https://localhost:44311'.
2023-07-25 16:21:39.5054|0|INFO|Microsoft.AspNetCore.Cors.Infrastructure.CorsService|CORS policy execution successful.
2023-07-25 16:21:39.5054|0|DEBUG|Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware|Static files was skipped as the request already matched an endpoint.
2023-07-25 16:21:53.3856|0|ERROR|TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware|Exception message: The client has disconnected Microsoft.AspNetCore.Connections.ConnectionResetException: The client has disconnected
---> System.Runtime.InteropServices.COMException (0x800704CD): Próbowano wykonać operację na nieistniejącym połączeniu sieciowym. (0x800704CD)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody()
at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
at System.IO.StreamReader.ReadToEndAsyncInternal(CancellationToken cancellationToken)
at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.ReadRequestBody(HttpRequest request) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 83
at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 23
2023-07-25 16:22:06.9989|0|ERROR|Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware|An unhandled exception has occurred while executing the request. Microsoft.AspNetCore.Connections.ConnectionResetException: The client has disconnected
---> System.Runtime.InteropServices.COMException (0x800704CD): Próbowano wykonać operację na nieistniejącym połączeniu sieciowym. (0x800704CD)
--- End of inner exception stack trace ---
at Microsoft.AspNetCore.Server.IIS.Core.IO.AsyncIOOperation.GetResult(Int16 token)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadBody()
at System.IO.Pipelines.Pipe.GetReadResult(ReadResult& result)
at System.IO.Pipelines.Pipe.ReadAsync(CancellationToken token)
at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContext.ReadAsync(Memory`1 memory, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.IIS.Core.HttpRequestStream.ReadAsyncInternal(Memory`1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.WebUtilities.FileBufferingReadStream.ReadAsync(Memory`1 buffer, CancellationToken cancellationToken)
at System.IO.StreamReader.ReadBufferAsync(CancellationToken cancellationToken)
at System.IO.StreamReader.ReadToEndAsyncInternal(CancellationToken cancellationToken)
at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.ReadRequestBody(HttpRequest request) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 83
at TimeboxTA.WebAPI.Middleware.ErrorHandlingMiddleware.InvokeAsync(HttpContext context, RequestDelegate next) in C:\Users\lukasz.strus\source\repos\bmroz\TimeboxTA\TimeboxTA.WebAPI\Middleware\ErrorHandlingMiddleware.cs:line 45
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<<UseMiddlewareInterface>b__1>d.MoveNext()
--- End of stack trace from previous location ---
at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
at Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware.Invoke(HttpContext httpContext)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)
2023-07-25 16:22:07.0112|0|INFO|Microsoft.AspNetCore.Hosting.Diagnostics|Request finished HTTP/2 POST https://localhost:44344/api/task-types application/json 70 - 500 - text/plain;+charset=utf-8 27503.0152ms
I tried to ignore this exception, but it didn't help.
I found the problem. In the html file, the button had the "submit" type (the fault of mindless copying and pasting of code fragments). As a result, it could not send a query to the server, and if it somehow managed to read the body on the backand side, it lost the connection.