Sure, here is the code to send an HTTP POST message in ASP.NET Core using HttpClient.PostAsJsonAsync:
C#
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
Install-Package Microsoft.AspNetCore.Http
Once the package is installed, you can run the code by pressing F5 in Visual Studio.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is the code to send an HTTP POST message in ASP.NET Core using HttpClient.PostAsJsonAsync:
C#
This code will first create a
HttpClientinstance. Then, it will create a JSON object to send as the request body. The JSON object will contain aNameproperty and anAgeproperty. TheNameproperty will be set to "John Doe" and theAgeproperty 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
Once the package is installed, you can run the code by pressing
F5in Visual Studio.