---
title: "How to send HTTP POST message in ASP.NET Core using HttpClient Post as Json Async?"  
description: "How to send HTTP POST message in ASP.NET Core using HttpClient Post as Json Async?"  
author: "Utpal Vishwas"  
published: 2023-07-06  
updated: 2023-07-07  
canonical: https://www.mindstick.com/forum/158981/how-to-send-http-post-message-in-asp-dot-net-core-using-httpclient-post-as-json-async  
category: ".net core"  
tags: ["asp.net", "asp.net core", ".net core"]  
reading_time: 2 minutes  

---

# How to send HTTP POST message in ASP.NET Core using HttpClient Post as Json Async?

How to send [HTTP](https://www.mindstick.com/blog/217/http-endpoints-in-sql-server-2005-2008) [POST](https://www.mindstick.com/articles/12829/aspects-that-make-australia-post-shipping-extension-a-useful-tool) [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) in [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio) using HttpClient Post as [Json](https://www.mindstick.com/forum/34446/convert-json-string-to-object) [Async](https://www.mindstick.com/forum/23036/calling-an-activity-class-from-async-class)?

## Replies

### Reply by Aryan Kumar

Sure, here is the code to send an HTTP POST message in [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core using HttpClient.PostAsJsonAsync:

C#

```plaintext
using System.Net.Http;
using System.Threading.Tasks;

namespace MyApp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            // Create a HttpClient instance.
            var client = new HttpClient();

            // Create a JSON object to send as the request body.
            var jsonObject = new {
                Name = "John Doe",
                Age = 30
            };

            // Serialize the JSON object to a string.
            var jsonString = JsonConvert.SerializeObject(jsonObject);

            // Create a StringContent object to hold the JSON string.
            var content = new StringContent(jsonString, Encoding.UTF8, "application/json");

            // Send the POST request.
            var response = await client.PostAsync("https://example.com/api/v1/users", content);

            // Check the response status code.
            if (response.StatusCode == HttpStatusCode.OK)
            {
                // The request was successful.
            }
            else
            {
                // The request failed.
            }
        }
    }
}
```

This code will first create a `HttpClient` instance. Then, it will create a JSON object to send as the request body. The JSON object will contain a `Name` property and an `Age` property. The `Name` property will be set to "John Doe" and the `Age` property will be set to 30.

Next, the code will serialize the JSON object to a string. The string will be encoded as UTF-8 and the content type will be set to "application/json".

Finally, the code will send the POST request to the specified URL. The response status code will be checked to determine if the request was successful. If the request was successful, the code will do nothing. Otherwise, the code will print an error message.

To run this code, you will need to install the `Microsoft.AspNetCore.Http NuGet package`. You can do this by opening the Package Manager Console in Visual Studio and running the following command:

Code snippet

```plaintext
Install-Package Microsoft.AspNetCore.Http
```

Once the package is installed, you can run the code by pressing `F5` in Visual Studio.


---

Original Source: https://www.mindstick.com/forum/158981/how-to-send-http-post-message-in-asp-dot-net-core-using-httpclient-post-as-json-async

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
