-
Notifications
You must be signed in to change notification settings - Fork 17
HttpRequestMessage Examples
mozhganEtaati edited this page Oct 9, 2025
·
9 revisions
💡 The examples below use POST requests via the HttpRequestMessage extension, but the package also supports GET, PUT, DELETE, and PATCH, and works with JSON, XML, or other content types.
using System;
using System.Net.Http;
using System.Text;
using HttpClientToCurl;
class Program
{
static void Main()
{
string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}";
var requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = newStringContent(requestBody, Encoding.UTF8,MediaTypeNames.Application.Json) };
httpRequestMessage.Headers.Add("Authorization", "Bearer 4797c126-3f8a-454a-aff1-96c0220dae61");
var baseAddress = new Uri("http://localhost:1213/v1/");
string curl = httpRequestMessage.GenerateCurlInString(baseAddress);
Console.WriteLine("Generated curl command:\n");
Console.WriteLine(curl);
}
}using System.Text;
using System.Text.Json;
using HttpClientToCurl;
class Program
{
static async Task Main()
{
string apiUrl = "https://jsonplaceholder.typicode.com/posts";
// Create an instance of HttpClient
HttpClient client = new();
try
{
// Create a sample JSON payload using a strongly-typed object
var payload = new
{
title = "New Post",
body = "This is the body of the new post",
userId = 1
};
// Serialize the object to a JSON string
string jsonPayload = JsonSerializer.Serialize(payload);
// Create HttpRequestMessage with the POST verb
HttpRequestMessage request = new(HttpMethod.Post, apiUrl);
// Set up the request headers
request.Headers.Add("Authorization", "Bearer YourAccessToken"); // Add any necessary headers
// Set the request content with the JSON payload
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
var baseAddress = new Uri("https://jsonplaceholder.typicode.com");
// Generate a curl command and write it to a file for debugging or testing.
// This command can be imported into Postman for checking and comparing against all the requirements.
// Config is optional
request.GenerateCurlInFile(baseAddress, config =>
{
// Customize file configuration if needed
config.TurnOn = true; // Enable generating curl command to file
config.Filename = "curl_commands.txt"; // Specify the file name
config.Path = "C:\\Path\\To\\Directory"; // Specify the directory path
config.NeedAddDefaultHeaders = true; // Specify if default headers should be included
});
// Send the request
HttpResponseMessage response = await client.SendAsync(request);
// Check if the request was successful (status code 200-299)
if (response.IsSuccessStatusCode)
{
// Read and print the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response from the API:\n" + responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}using System.Text;
using System.Text.Json;
using HttpClientToCurl;
class Program
{
static async Task Main()
{
string apiUrl = "https://jsonplaceholder.typicode.com/posts";
// Create an instance of HttpClient
HttpClient client = new();
try
{
// Create a sample JSON payload using a strongly-typed object
var payload = new
{
title = "New Post",
body = "This is the body of the new post",
userId = 1
};
// Serialize the object to a JSON string
string jsonPayload = JsonSerializer.Serialize(payload);
// Create HttpRequestMessage with the POST verb
HttpRequestMessage request = new(HttpMethod.Post, apiUrl);
// Set up the request headers
request.Headers.Add("Authorization", "Bearer YourAccessToken"); // Add any necessary headers
// Set the request content with the JSON payload
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
var baseAddress = new Uri("https://jsonplaceholder.typicode.com");
// Generate a curl command and write it to a file for debugging or testing.
// This command can be imported into Postman for checking and comparing against all the requirements.
// Config is optional
request.GenerateCurlInConsole(baseAddress, config => { config.EnableCodeBeautification = true; });
// Send the request
HttpResponseMessage response = await client.SendAsync(request);
// Check if the request was successful (status code 200-299)
if (response.IsSuccessStatusCode)
{
// Read and print the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine("Response from the API:\n" + responseBody);
}
else
{
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex.Message}");
}
}
}HttpClientToCurl Document