using System.Net.Http; var client = new HttpClient(); var request = new HttpRequestMessage(HttpMethod.Get, "https://jsonplaceholder.typicode.com/posts/1"); var response = await client.SendAsync(request); var content = await response.Content.ReadAsStringAsync(); Console.WriteLine(content);
using System.Net.Http; using System.Text.Json; var client = new HttpClient(); var post = new { title = "My new post", body = "Lorem ipsum" }; var json = JsonSerializer.Serialize(post); var content = new StringContent(json, System.Text.Encoding.UTF8, "application/json"); var request = new HttpRequestMessage(HttpMethod.Post, "https://jsonplaceholder.typicode.com/posts") { Content = content }; var response = await client.SendAsync(request); var result = await response.Content.ReadAsStringAsync(); Console.WriteLine(result);This code creates a new HttpClient, sets up an anonymous object to hold the data for the new post, serializes it to JSON using the System.Text.Json namespace, and creates a new StringContent object with the JSON content and content type. It then creates an HttpRequestMessage to send a POST request to the REST API, sets the content to the JSON string, sends the request, and reads the response content. Package Library: System.Text.Json, System.Net.Http