how to get post body in httpContext as string

mc 5,426 Reputation points
2023-05-20T10:47:09.77+00:00

how to get all post data in HttpContext?

Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2023-05-21T07:28:20.08+00:00

    It should be part of Request.Body

    See the example:

    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;
    
    public class MyController : Controller
    {
        [HttpPost]
        public IActionResult MyAction()
        {
            // Read the post data from the request body
            using (var reader = new StreamReader(HttpContext.Request.Body))
            {
                var postData = reader.ReadToEnd();
                // Process the post data as needed
                // ...
            }
    
            // Return a response
            return Ok();
        }
    }
    
    2 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Vahid Ghafarpour 23,385 Reputation points Volunteer Moderator
    2023-05-21T07:29:19.3866667+00:00

    you can access the post data by reading the HttpContext.Request.Body stream using a StreamReader.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.