I’m trying to read the body of a request but I always get an empty string, I’m using Asp.Net Web Api with .Net 8.0 and I do this based on the following code:
[Authorize]
[ApiController]
[Route("api/[controller]/[action]")]
public class StatusWAController : ControllerBase
{
private readonly ILogger<StatusWAController> _logger;
private readonly IstatusMensajeWAService _statusMensajeWAService;
public StatusWAController(ILogger<StatusWAController> logger, IstatusMensajeWAService statusMensajeWAService)
{
_logger = logger;
_statusMensajeWAService = statusMensajeWAService;
}
[HttpPost(Name = "CreateResponseWA")]
public async Task<IActionResult> CreateResponseWA(CreateResponseWADto createResponseWADto)
{
_logger.LogWarning("Request started: ");
Request.EnableBuffering();
Request.Body.Position = 0;
var body = await new StreamReader(Request.Body).ReadToEndAsync(); //Always empty
_logger.LogWarning("Request finished: " + body);//Only shows --> "Request finished: "
return Ok(body)
}
}
I will appreciate the correct way to read the request body.