HttpClientToCurl 2.0.6

dotnet add package HttpClientToCurl --version 2.0.6                
NuGet\Install-Package HttpClientToCurl -Version 2.0.6                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HttpClientToCurl" Version="2.0.6" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpClientToCurl --version 2.0.6                
#r "nuget: HttpClientToCurl, 2.0.6"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HttpClientToCurl as a Cake Addin
#addin nuget:?package=HttpClientToCurl&version=2.0.6

// Install HttpClientToCurl as a Cake Tool
#tool nuget:?package=HttpClientToCurl&version=2.0.6                

🥇 HttpClientToCurlGenerator 🥇

An extension for generating the Curl script of HttpClient in .NET

license forks stars
example workflow

This extension will help you to see whatever is set in HttpClient in the form of a curl script.

And you can check if that is the correct data for sending to an external service or not. also if you have an error, you can check the script find your problem, and fix that. so easily.

Also, it is the new way and fast way to create or update a collection of Postman, when you haven't got a postman collection for your desired external service.

It's easy to use. just you should install the package on your project from the below address and use sample codes for how to call and work with extensions.

For adding a package to your project from Nuget use this command

dotnet add package HttpClientToCurl 

Nuget Package Address

You have 3 ways to see script result:

1: Put it in a string variable:

string curlScript = httpClient.GenerateCurlInString(httpRequestMessage);

2: Show to the IDE console:

httpClient.GenerateCurlInConsole(httpRequestMessage);
  • Notice: when the curl script was written in the console, maybe your IDE console applies WordWrap automatically. you should remove enters from the script.
  • Notice: The 'config' Parameter is optional.

3: Write in a file:

httpClient.GenerateCurlInFile(httpRequestMessage);
  • Notice: The 'config' Parameter is optional.

Read more about this extension:

English Article

Persian Article

Please let me know if you have any feedback and your solution to improve the code and also if you find a problem. also, I will be extremely happy if you contribute to the implementation and improvement of the project.

Gmail Address

Give a Star!

If you like this project, learn something, or are using it in your applications, please give it a star. Thanks!

How to use HttpClientToCurlGenerator Extensions:

You can see more samples in the FunctionalTest Directory.

Post Method sample code (it will be written in the console):

string requestBody = @"{""name"":""amin"",""requestId"":""10001000"",""amount"":10000}";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

 // Call PostAsync => await client.PostAsync(requestUri, httpRequestMessage.Content);

Post Method sample code for FormUrlEncodedContent (it will be written in the console):

string requestBody = @"{""name"":""justin"",""requestId"":10001026,""amount"":26000}";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
    httpRequestMessage.Content = new FormUrlEncodedContent(new[]
{
    new KeyValuePair<string, string>("session", "703438f3-16ad-4ba5-b923-8f72cd0f2db9"),
    new KeyValuePair<string, string>("payload", requestBody),
});
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

// Call PostAsync => await client.PostAsync(requestUri, httpRequestMessage.Content);

Post Method sample code for XML (it will be written in the console):

string requestBody = @"<?xml version = ""1.0"" encoding = ""UTF-8""?>
    <Order>
    <Id>12</Id>
    <name>Jason</name>
    <requestId>10001024</requestId>
    <amount>240000</amount>
    </Order>";

var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/test");
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config: config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

// Call PostAsync => await client.PostAsync(requestUri, httpRequestMessage.Content);

Post Method sample code (it will be written in the file):

If the path variable is null or empty, then the file is created in the root project.

If the filename variable is null or empty, then the current date will be set for it with this format: yyyyMMdd

string path = string.Empty;
string filename = "PostMethodResult" ;
string requestBody = @"{""name"":""sara"",""requestId"":""10001001"",""amount"":20000}";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInFile(
    httpRequestMessage,
    config =>
    {
        config.Filename = filename;
        config.Path = path;
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
    });

// Call PostAsync => await client.PostAsync(requestUri, httpRequestMessage.Content);

Get Method sample code (it will be written in the console):

string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

// Call GetAsync => await client.GetAsync(requestUri);

Get Method sample code (it will be written in the file):

If the path variable is null or empty, then the file is created in the root project.

If the filename variable is null or empty, then the current date will be set for it with this format: yyyyMMdd

string path = string.Empty;
string filename = "GetMethodResult";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInFile(
    httpRequestMessage,
    config =>
    {
        config.Filename = filename;
        config.Path = path;
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
    });

// Call GetAsync => await client.GetAsync(requestUri);

Put Method sample code (it will be written in the console):

string requestBody = @"{""name"":""jadi"",""requestId"":""10001003"",""amount"":30000}";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

// Call PutAsync => await client.PutAsync(requestUri, httpRequestMessage.Content);

Put Method sample code (it will be written in the file):

If the path variable is null or empty, then the file is created in the root project.

If the filename variable is null or empty, then the current date will be set for it with this format: yyyyMMdd

string path = string.Empty;
string filename = "PutMethodResult" ;
string requestBody = @"{ ""name"" : ""reza"",""requestId"" : ""10001004"",""amount"":40000 }";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Put, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInFile(
    httpRequestMessage,
    config =>
    {
        config.Filename = filename;
        config.Path = path;
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
    });

// Call PutAsync => await client.PutAsync(requestUri, httpRequestMessage.Content);

Patch Method sample code (it will be written in the console):

string requestBody = @"{""name"":""hamed"",""requestId"":""10001005"",""amount"":50000}";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

// Call PatchAsync => await client.PatchAsync(requestUri, httpRequestMessage.Content);

Patch Method sample code (it will be written in the file):

If the path variable is null or empty, then the file is created in the root project.

If the filename variable is null or empty, then the current date will be set for it with this format: yyyyMMdd

string path = string.Empty;
string filename = "PatchMethodResult" ;
string requestBody = @"{ ""name"" : ""zara"",""requestId"" : ""10001006"",""amount"":60000 }";
string requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Patch, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInFile(
    httpRequestMessage,
    config =>
    {
        config.Filename = filename;
        config.Path = path;
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
    });

// Call PatchAsync => await client.PatchAsync(requestUri, httpRequestMessage.Content);

Delete Method sample code (it will be written in the console):

int id = 12;
string requestUri = $"api/test/{id}";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, requestUri);
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

// Call DeleteAsync => await client.DeleteAsync(requestUri);

Delete Method sample code (it will be written in the file):

If the path variable is null or empty, then the file is created in the root project.

If the filename variable is null or empty, then the current date will be set for it with this format: yyyyMMdd

string path = string.Empty;
string filename = "DeleteMethodResult";
int id = 12;
string requestUri = $"api/test/{id}";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Delete, requestUri);
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

httpClient.GenerateCurlInFile(
    httpRequestMessage,
    config =>
    {
        config.Filename = filename;
        config.Path = path;
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
    });

// Call DeleteAsync => await client.DeleteAsync(requestUri);

You can see more samples in the Functional Tests Directory.

I hope you enjoy this extension in your projects.

All Thanks to Our Contributors:

<a href="https://github.com/amingolmahalle/HttpClientToCurlGenerator/graphs/contributors"> <img src="https://contrib.rocks/image?repo=amingolmahalle/HttpClientToCurlGenerator" /> </a>

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on HttpClientToCurl:

Package Downloads
SomeGenericDev.RestSharpToCurl

A tiny and hacky library that uses reflection and DelegatingHandlers to generate a cURL script out of RestSharp's requests.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.6 123,672 3/13/2024
2.0.5 24,709 1/19/2024
2.0.3 8,636 12/20/2023
2.0.2 74,934 10/1/2023
2.0.1 5,100 8/13/2023
2.0.0 16,500 5/25/2023
1.9.0 18,890 3/13/2023
1.8.9 531 3/1/2023
1.8.8 2,917 2/9/2023
1.8.7 563 2/7/2023
1.8.6 518 2/6/2023
1.8.5 557 1/25/2023
1.8.3 861 11/23/2022
1.8.2 594 10/31/2022
1.8.1 683 9/19/2022
1.8.0 701 9/19/2022
1.7.0 725 9/13/2022
1.6.0 656 9/13/2022
1.5.0 698 9/11/2022
1.4.0 666 9/10/2022
1.3.0 651 9/6/2022
1.2.0 667 9/5/2022
1.1.0 640 9/5/2022
1.0.0 925 9/1/2022

2.0.6