From cd17bcbcd181a8b61fdf6021e44568bfced55164 Mon Sep 17 00:00:00 2001 From: Amirhossein Enayati <61790745+AmirhosseinEnayati@users.noreply.github.com> Date: Fri, 7 Nov 2025 13:28:31 +0330 Subject: [PATCH 1/4] Add 'Global Registration' and 'Specific Registration' --- README.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/README.md b/README.md index f78eac8..2e65c78 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,43 @@ For full examples, detailed usage, and advanced configuration options, please se ๐Ÿ‘‰ [Open Wiki โ†’ More Details](https://github.com/amingolmahalle/HttpClientToCurlGenerator/wiki) ## ๐Ÿš€ **Usage Example** + +### โšก Quick Usage + +#### 1๏ธโƒฃ Global Registration + +Enable cURL generation globally so that every `HttpClient` created through `IHttpClientFactory` automatically logs cURL commands before sending requests. + +**Program.cs / Startup:** + +```csharp +using HttpClientToCurl; + +// Register global cURL generation mode +builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration); + +// Register a default HttpClient (now cURL-enabled) +builder.Services.AddHttpClient(); +``` + +#### 2๏ธโƒฃ Specific Registration + +If you only want cURL generation for specific clients, you can enable it per-client easily using the built-in registration helpers. + +**Program.cs / Startup:** + +```csharp +using HttpClientToCurl; + +// Register the cURL generator service once +builder.Services.AddHttpClientToCurl(builder.Configuration); + +// Then register specific HttpClients with cURL logging enabled +builder.Services.AddHttpClient("my-client1", showCurl: true); +``` + +### โš™๏ธ Manual Usage + ```csharp using System.Text; using HttpClientToCurl; From df31557b62eba67d38cabe2ad442067116d146bc Mon Sep 17 00:00:00 2001 From: Amirhossein Enayati <61790745+AmirhosseinEnayati@users.noreply.github.com> Date: Fri, 7 Nov 2025 13:51:18 +0330 Subject: [PATCH 2/4] Add configuration for global and specific usage --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/README.md b/README.md index 2e65c78..493e4aa 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,29 @@ builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration); builder.Services.AddHttpClient(); ``` +##### Configuration (via `appsettings.json`) + +```json +"HttpClientToCurl": { + "TurnOnAll": true, + + "ShowOnConsole": { + "TurnOn": true, + "NeedAddDefaultHeaders": true, + "EnableCompression": false, + "EnableCodeBeautification": true + }, + + "SaveToFile": { + "TurnOn": true, + "NeedAddDefaultHeaders": true, + "EnableCompression": false, + "Filename": "curl_commands", + "Path": "C:\\Users\\Public" + } + } +``` + #### 2๏ธโƒฃ Specific Registration If you only want cURL generation for specific clients, you can enable it per-client easily using the built-in registration helpers. @@ -71,6 +94,29 @@ builder.Services.AddHttpClientToCurl(builder.Configuration); builder.Services.AddHttpClient("my-client1", showCurl: true); ``` +##### Configuration (via `appsettings.json`) + +```json +"HttpClientToCurl": { + "TurnOnAll": true, + + "ShowOnConsole": { + "TurnOn": true, + "NeedAddDefaultHeaders": true, + "EnableCompression": false, + "EnableCodeBeautification": true + }, + + "SaveToFile": { + "TurnOn": true, + "NeedAddDefaultHeaders": true, + "EnableCompression": false, + "Filename": "curl_commands", + "Path": "C:\\Users\\Public" + } + } +``` + ### โš™๏ธ Manual Usage ```csharp From 2b4652a6e74a54d9af67b82b9da1e638c359260e Mon Sep 17 00:00:00 2001 From: Amirhossein Enayati <61790745+AmirhosseinEnayati@users.noreply.github.com> Date: Fri, 7 Nov 2025 19:33:51 +0330 Subject: [PATCH 3/4] Revise README for improved clarity and examples Updated README.md to enhance clarity and detail about the HttpClientToCurl library, including usage examples and installation instructions. --- README.md | 184 +++++++++++++++++++++++++++++------------------------- 1 file changed, 100 insertions(+), 84 deletions(-) diff --git a/README.md b/README.md index 493e4aa..8da7068 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # ๐Ÿฅ‡ HttpClientToCurl -An extension for generating the **CURL script** from **`HttpClient`** and **`HttpRequestMessage`** in .NET. +Generate cURL commands directly from your `HttpClient` or `HttpRequestMessage` in .NET โ€” perfect for debugging, logging, and sharing HTTP requests. + +--- + +### ๐Ÿ“Š Badges [![license](https://img.shields.io/github/license/amingolmahalle/HttpClientToCurlGenerator)](https://github.com/amingolmahalle/HttpClientToCurlGenerator/blob/master/LICENSE) [![stars](https://img.shields.io/github/stars/amingolmahalle/HttpClientToCurlGenerator)](https://github.com/amingolmahalle/HttpClientToCurlGenerator/stargazers) [![NuGet Version](https://img.shields.io/nuget/v/HttpClientToCurl.svg)](https://www.nuget.org/packages/HttpClientToCurl/) @@ -10,114 +14,120 @@ An extension for generating the **CURL script** from **`HttpClient`** and **`Htt --- ## ๐Ÿ“– Overview -**`HttpClientToCurl`** is a lightweight **.NET extension library** that helps you visualize any HTTP request as a **CURL command**. -You can use its extension methods on both: +**HttpClientToCurl** is a lightweight and powerful .NET extension library that turns your HTTP requests into **cURL** commands. + +You can use its extension methods on: - **`HttpClient`** โ€” to generate CURL directly when sending requests - **`HttpRequestMessage`** โ€” to inspect or log CURL representations before sending -This is useful for: -- Debugging and verifying request payloads or headers -- Sharing API calls between teammates -- Generating or updating Postman collections easily +--- + +### ๐Ÿงฉ 1. Automatic Mode + +Automatically generates cURL output whenever your app sends a request. +You can configure it through dependency injection: + +- **Global Registration** โ€” enable for all `HttpClient` instances created via `IHttpClientFactory` +- **Per-Client Registration** โ€” enable only for selected clients + +**Best for:** +Logging, monitoring, or tracing outgoing requests across the application. --- + +### ๐Ÿงฐ 2. Manual Mode + +Generate cURL commands **on demand** using extension methods on either `HttpClient` or `HttpRequestMessage`. + +**Best for:** +Debugging individual requests, creating reproducible Postman calls, or sharing API examples. + +--- + +### ๐Ÿ’ก Why Use HttpClientToCurl? + +- ๐Ÿงช Instantly visualize and debug request payloads or headers +- ๐Ÿค Share exact API calls with teammates or QA engineers +- โš™๏ธ Simplify Postman and CLI reproduction +- ๐Ÿชถ Lightweight, dependency-free, and easy to integrate + +--- + ## โš™๏ธ Installation +Install via NuGet: + ```bash dotnet add package HttpClientToCurl ``` -Or visit the NuGet page here: HttpClientToCurl - -## ๐Ÿ“š Documentation -For full examples, detailed usage, and advanced configuration options, please see the **Wiki**: +Or visit the [NuGet page โ†’](https://www.nuget.org/packages/HttpClientToCurl) -๐Ÿ‘‰ [Open Wiki โ†’ More Details](https://github.com/amingolmahalle/HttpClientToCurlGenerator/wiki) - -## ๐Ÿš€ **Usage Example** - -### โšก Quick Usage +--- -#### 1๏ธโƒฃ Global Registration +## ๐Ÿš€ Quick Start -Enable cURL generation globally so that every `HttpClient` created through `IHttpClientFactory` automatically logs cURL commands before sending requests. +### ๐Ÿ”ง 1๏ธโƒฃ Global Registration -**Program.cs / Startup:** +Enable cURL generation globally โ€” every `HttpClient` created through `IHttpClientFactory` will automatically log cURL commands. +**Program.cs / Startup.cs** ```csharp using HttpClientToCurl; -// Register global cURL generation mode +// Register global cURL generation builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration); -// Register a default HttpClient (now cURL-enabled) +// Register default HttpClient (now cURL-enabled) builder.Services.AddHttpClient(); ``` -##### Configuration (via `appsettings.json`) - +**appsettings.json** ```json "HttpClientToCurl": { - "TurnOnAll": true, - - "ShowOnConsole": { - "TurnOn": true, - "NeedAddDefaultHeaders": true, - "EnableCompression": false, - "EnableCodeBeautification": true - }, - - "SaveToFile": { - "TurnOn": true, - "NeedAddDefaultHeaders": true, - "EnableCompression": false, - "Filename": "curl_commands", - "Path": "C:\\Users\\Public" - } + "TurnOnAll": true, // Master switch: enable or disable the entire HttpClientToCURL logging system + + "ShowOnConsole": { + "TurnOn": true, // Enable console output for generated curl commands + "NeedAddDefaultHeaders": true, // Include default headers (like User-Agent, Accept, etc.) in the curl output + "EnableCompression": false, // Compress the console log output (not recommended for debugging readability) + "EnableCodeBeautification": true // Beautify and format the curl command for better readability + }, + + "SaveToFile": { + "TurnOn": true, // Enable saving the generated curl commands into a file + "NeedAddDefaultHeaders": true, // Include default headers (like User-Agent, Accept, etc.) in the curl output + "EnableCompression": false, // Compress the saved file (useful if logging a large number of requests) + "Filename": "curl_commands", // Name of the output file without extension (e.g., will produce curl_commands.log) + "Path": "C:\\Users\\Public" // Directory path where the log file will be created } +} ``` -#### 2๏ธโƒฃ Specific Registration +--- -If you only want cURL generation for specific clients, you can enable it per-client easily using the built-in registration helpers. +### ๐Ÿ”ง 2๏ธโƒฃ Per-Client Registration -**Program.cs / Startup:** +Enable cURL logging for specific named clients only. +**Program.cs / Startup.cs** ```csharp using HttpClientToCurl; -// Register the cURL generator service once +// Register the cURL generator once builder.Services.AddHttpClientToCurl(builder.Configuration); -// Then register specific HttpClients with cURL logging enabled +// Enable cURL logging for selected clients builder.Services.AddHttpClient("my-client1", showCurl: true); ``` -##### Configuration (via `appsettings.json`) +**appsettings.json** +(same configuration options as above) -```json -"HttpClientToCurl": { - "TurnOnAll": true, - - "ShowOnConsole": { - "TurnOn": true, - "NeedAddDefaultHeaders": true, - "EnableCompression": false, - "EnableCodeBeautification": true - }, - - "SaveToFile": { - "TurnOn": true, - "NeedAddDefaultHeaders": true, - "EnableCompression": false, - "Filename": "curl_commands", - "Path": "C:\\Users\\Public" - } - } -``` +--- -### โš™๏ธ Manual Usage +## ๐Ÿงฐ Manual Usage Example ```csharp using System.Text; @@ -127,20 +137,22 @@ class Program { static async Task Main() { + var baseAddress = new Uri("http://localhost:1213/v1/"); var requestUri = "api/test"; - using var httpClientInstance = new HttpClient(); - var baseAddress = new Uri("http://localhost:1213/v1/"); - httpClientInstance.BaseAddress = baseAddress; - string requestBody = /*lang=json,strict*/ @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; - HttpRequestMessage httpRequestMessageInstance = new(HttpMethod.Post, requestUri); + using var httpClientInstance = new HttpClient { BaseAddress = baseAddress }; + + string requestBody = @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; + var httpRequestMessageInstance = new HttpRequestMessage(HttpMethod.Post, requestUri) + { + Content = new StringContent(requestBody, Encoding.UTF8, "application/json") + }; httpRequestMessageInstance.Headers.Add("Authorization", "Bearer YourAccessToken"); - httpRequestMessageInstance.Content = new StringContent(requestBody, Encoding.UTF8, "application/json"); - // Using the HttpClient extension: + // Option 1: Generate cURL from HttpClient httpClientInstance.GenerateCurlInConsole(httpRequestMessageInstance); - // Or using the HttpRequestMessage extension: + // Option 2: Generate cURL from HttpRequestMessage httpRequestMessageInstance.GenerateCurlInConsole(baseAddress); await httpClientInstance.SendAsync(httpRequestMessageInstance); @@ -148,22 +160,26 @@ class Program } ``` -## โœ… Output: - +โœ… **Example Output** ```bash -curl -X POST 'http://localhost:1213/v1/api/test' -H 'Authorization: Bearer YourAccessToken' --H 'Content-Type: application/json; charset=utf-8' -d '{"name":"sara","requestId":10001001,"amount":20000}' +curl -X POST 'http://localhost:1213/v1/api/test' \ + -H 'Authorization: Bearer YourAccessToken' \ + -H 'Content-Type: application/json; charset=utf-8' \ + -d '{"name":"sara","requestId":10001001,"amount":20000}' ``` -## ๐Ÿงฉ **Other Features** - -Works with **GET**, **POST**, **PUT**, **PATCH**, and **DELETE** +--- -Supports **JSON**, **XML**, and **FormUrlEncodedContent** +## ๐Ÿงฉ Features -## **Output to:** +| Feature | Description | +|----------|--------------| +| ๐Ÿ” Methods | Supports `GET`, `POST`, `PUT`, `PATCH`, `DELETE` | +| ๐Ÿง  Content Types | JSON, XML, `FormUrlEncodedContent` | +| ๐Ÿ’พ Output | Console โ€ข File โ€ข String | +| ๐ŸŽจ Beautified Output | Optional pretty printing | -**Console** / **String** / **File** +--- ## ๐Ÿ“š Articles From d149378741fc64f76ef182b55d06a9ba111781df Mon Sep 17 00:00:00 2001 From: Amirhossein Enayati <61790745+AmirhosseinEnayati@users.noreply.github.com> Date: Sat, 8 Nov 2025 16:23:15 +0330 Subject: [PATCH 4/4] Update README.md --- README.md | 147 +++++++++++++++++++++++++++--------------------------- 1 file changed, 74 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 8da7068..57840d5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # ๐Ÿฅ‡ HttpClientToCurl -Generate cURL commands directly from your `HttpClient` or `HttpRequestMessage` in .NET โ€” perfect for debugging, logging, and sharing HTTP requests. +Generate curl commands directly from your `HttpClient` or `HttpRequestMessage` in .NET โ€” perfect for debugging, logging, and sharing HTTP requests. --- @@ -14,18 +14,23 @@ Generate cURL commands directly from your `HttpClient` or `HttpRequestMessage` i --- ## ๐Ÿ“– Overview +**HttpClientToCurl** is a lightweight and powerful .NET extension library that turns your HTTP requests into curl commands. +It works with both **`HttpClient`** and **`HttpRequestMessage`**, giving you two simple ways to generate curl commands: -**HttpClientToCurl** is a lightweight and powerful .NET extension library that turns your HTTP requests into **cURL** commands. +--- + +### ๐Ÿงฐ 1. Manual Mode -You can use its extension methods on: -- **`HttpClient`** โ€” to generate CURL directly when sending requests -- **`HttpRequestMessage`** โ€” to inspect or log CURL representations before sending +Generate curl commands **on demand** using extension methods on either `HttpClient` or `HttpRequestMessage`. + +**Best for:** +Debugging individual requests, creating reproducible Postman calls, or sharing API examples. --- -### ๐Ÿงฉ 1. Automatic Mode +### ๐Ÿงฉ 2. Automatic Mode -Automatically generates cURL output whenever your app sends a request. +Automatically generates curl output whenever your app sends a request. You can configure it through dependency injection: - **Global Registration** โ€” enable for all `HttpClient` instances created via `IHttpClientFactory` @@ -36,15 +41,6 @@ Logging, monitoring, or tracing outgoing requests across the application. --- -### ๐Ÿงฐ 2. Manual Mode - -Generate cURL commands **on demand** using extension methods on either `HttpClient` or `HttpRequestMessage`. - -**Best for:** -Debugging individual requests, creating reproducible Postman calls, or sharing API examples. - ---- - ### ๐Ÿ’ก Why Use HttpClientToCurl? - ๐Ÿงช Instantly visualize and debug request payloads or headers @@ -53,33 +49,80 @@ Debugging individual requests, creating reproducible Postman calls, or sharing A - ๐Ÿชถ Lightweight, dependency-free, and easy to integrate --- - ## โš™๏ธ Installation -Install via NuGet: - ```bash dotnet add package HttpClientToCurl ``` +Or visit the NuGet page here: HttpClientToCurl + +## ๐Ÿ“š Documentation + +For full examples, detailed usage, and advanced configuration options, please see the **Wiki**: -Or visit the [NuGet page โ†’](https://www.nuget.org/packages/HttpClientToCurl) +๐Ÿ‘‰ [Open Wiki โ†’ More Details](https://github.com/amingolmahalle/HttpClientToCurlGenerator/wiki) --- ## ๐Ÿš€ Quick Start -### ๐Ÿ”ง 1๏ธโƒฃ Global Registration +## ๐Ÿงฐ Manual Mode Usage Example + +```csharp +using System.Text; +using HttpClientToCurl; + +class Program +{ + static async Task Main() + { + var baseAddress = new Uri("http://localhost:1213/v1/"); + var requestUri = "api/test"; + + using var httpClientInstance = new HttpClient { BaseAddress = baseAddress }; + + string requestBody = @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; + var httpRequestMessageInstance = new HttpRequestMessage(HttpMethod.Post, requestUri) + { + Content = new StringContent(requestBody, Encoding.UTF8, "application/json") + }; + httpRequestMessageInstance.Headers.Add("Authorization", "Bearer YourAccessToken"); + + // Option 1: Generate curl from HttpClient + httpClientInstance.GenerateCurlInConsole(httpRequestMessageInstance); + + // Option 2: Generate curl from HttpRequestMessage + httpRequestMessageInstance.GenerateCurlInConsole(baseAddress); + + await httpClientInstance.SendAsync(httpRequestMessageInstance); + } +} +``` + +โœ… **Example Output** +```bash +curl -X POST 'http://localhost:1213/v1/api/test' \ + -H 'Authorization: Bearer YourAccessToken' \ + -H 'Content-Type: application/json; charset=utf-8' \ + -d '{"name":"sara","requestId":10001001,"amount":20000}' +``` + +--- + +## ๐Ÿงฉ Automatic Mode Usage Example -Enable cURL generation globally โ€” every `HttpClient` created through `IHttpClientFactory` will automatically log cURL commands. +### 1๏ธโƒฃ Global Registration + +Enable curl generation globally โ€” every `HttpClient` created through `IHttpClientFactory` will automatically log curl commands. **Program.cs / Startup.cs** ```csharp using HttpClientToCurl; -// Register global cURL generation +// Register global curl generation builder.Services.AddHttpClientToCurlInGeneralMode(builder.Configuration); -// Register default HttpClient (now cURL-enabled) +// Register default HttpClient (now curl-enabled) builder.Services.AddHttpClient(); ``` @@ -107,75 +150,33 @@ builder.Services.AddHttpClient(); --- -### ๐Ÿ”ง 2๏ธโƒฃ Per-Client Registration +### 2๏ธโƒฃ Per-Client Registration -Enable cURL logging for specific named clients only. +Enable curl logging for specific named clients only. **Program.cs / Startup.cs** ```csharp using HttpClientToCurl; -// Register the cURL generator once +// Register the curl generator once builder.Services.AddHttpClientToCurl(builder.Configuration); -// Enable cURL logging for selected clients +// Enable curl logging for selected clients builder.Services.AddHttpClient("my-client1", showCurl: true); ``` +--- **appsettings.json** (same configuration options as above) --- -## ๐Ÿงฐ Manual Usage Example - -```csharp -using System.Text; -using HttpClientToCurl; - -class Program -{ - static async Task Main() - { - var baseAddress = new Uri("http://localhost:1213/v1/"); - var requestUri = "api/test"; - - using var httpClientInstance = new HttpClient { BaseAddress = baseAddress }; - - string requestBody = @"{""name"":""sara"",""requestId"":10001001,""amount"":20000}"; - var httpRequestMessageInstance = new HttpRequestMessage(HttpMethod.Post, requestUri) - { - Content = new StringContent(requestBody, Encoding.UTF8, "application/json") - }; - httpRequestMessageInstance.Headers.Add("Authorization", "Bearer YourAccessToken"); - - // Option 1: Generate cURL from HttpClient - httpClientInstance.GenerateCurlInConsole(httpRequestMessageInstance); - - // Option 2: Generate cURL from HttpRequestMessage - httpRequestMessageInstance.GenerateCurlInConsole(baseAddress); - - await httpClientInstance.SendAsync(httpRequestMessageInstance); - } -} -``` - -โœ… **Example Output** -```bash -curl -X POST 'http://localhost:1213/v1/api/test' \ - -H 'Authorization: Bearer YourAccessToken' \ - -H 'Content-Type: application/json; charset=utf-8' \ - -d '{"name":"sara","requestId":10001001,"amount":20000}' -``` - ---- - ## ๐Ÿงฉ Features | Feature | Description | |----------|--------------| | ๐Ÿ” Methods | Supports `GET`, `POST`, `PUT`, `PATCH`, `DELETE` | -| ๐Ÿง  Content Types | JSON, XML, `FormUrlEncodedContent` | +| ๐Ÿง  Content Types | `JSON`, `XML`, `FormUrlEncodedContent` | | ๐Ÿ’พ Output | Console โ€ข File โ€ข String | | ๐ŸŽจ Beautified Output | Optional pretty printing | @@ -183,7 +184,7 @@ curl -X POST 'http://localhost:1213/v1/api/test' \ ## ๐Ÿ“š Articles -- [How to Generate cURL Script of the HttpClient in .NET](https://www.c-sharpcorner.com/article/how-to-generate-curl-script-of-the-httpclient-in-net/) +- [How to Generate curl Script of the HttpClient in .NET](https://www.c-sharpcorner.com/article/how-to-generate-curl-script-of-the-httpclient-in-net/) - [New Feature in HttpClientToCurl for .NET: Debugging HttpRequestMessage Made Easy](https://medium.com/@mozhgan.etaati/new-feature-in-httpclienttocurl-for-net-debugging-httprequestmessage-made-easy-18cb66dd55f0)