Avatar ·

How to send HTTP POST request in .NET (C#) with JSON body?

Hi everyone! I'm a beginner in C# and .NET, and I can't figure out how to send POST requests. I need to send data to the server in JSON format, but all the examples I find online use the outdated `HttpWebRequest` class or some obscure libraries. Here's what I'm trying to do: I have a simple data class: ```csharp public class UserData { public string Name { get; set; } public int Age { get; set; } } ``` And I want to send this object to the URL `https://api.example.com/users` using the POST method. The problem is that I don't understand how to properly serialize the object to JSON, how to set headers (Content-Type), and how to read the response from the server. I tried using `HttpWebRequest`, but the code turns out very cumbersome, and I keep getting errors.