11# 🥇 HttpClientToCurl
2- An extension for generating the ** CURL script** from ** ` HttpClient ` ** and ** ` HttpRequestMessage ` ** in .NET.
32
3+ Generate curl commands directly from your ` HttpClient ` or ` HttpRequestMessage ` in .NET — perfect for debugging, logging, and sharing HTTP requests.
4+
5+ ---
6+
7+ ### 📊 Badges
48[ ![ license] ( https://img.shields.io/github/license/amingolmahalle/HttpClientToCurlGenerator )] ( https://github.com/amingolmahalle/HttpClientToCurlGenerator/blob/master/LICENSE )
59[ ![ stars] ( https://img.shields.io/github/stars/amingolmahalle/HttpClientToCurlGenerator )] ( https://github.com/amingolmahalle/HttpClientToCurlGenerator/stargazers )
610[ ![ NuGet Version] ( https://img.shields.io/nuget/v/HttpClientToCurl.svg )] ( https://www.nuget.org/packages/HttpClientToCurl/ )
@@ -10,16 +14,39 @@ An extension for generating the **CURL script** from **`HttpClient`** and **`Htt
1014---
1115
1216## 📖 Overview
13- ** ` HttpClientToCurl ` ** is a lightweight ** .NET extension library** that helps you visualize any HTTP request as a ** CURL command** .
17+ ** HttpClientToCurl** is a lightweight and powerful .NET extension library that turns your HTTP requests into curl commands.
18+ It works with both ** ` HttpClient ` ** and ** ` HttpRequestMessage ` ** , giving you two simple ways to generate curl commands:
19+
20+ ---
21+
22+ ### 🧰 1. Manual Mode
23+
24+ Generate curl commands ** on demand** using extension methods on either ` HttpClient ` or ` HttpRequestMessage ` .
25+
26+ ** Best for:**
27+ Debugging individual requests, creating reproducible Postman calls, or sharing API examples.
28+
29+ ---
30+
31+ ### 🧩 2. Automatic Mode
32+
33+ Automatically generates curl output whenever your app sends a request.
34+ You can configure it through dependency injection:
35+
36+ - ** Global Registration** — enable for all ` HttpClient ` instances created via ` IHttpClientFactory `
37+ - ** Per-Client Registration** — enable only for selected clients
1438
15- You can use its extension methods on both:
16- - ** ` HttpClient ` ** — to generate CURL directly when sending requests
17- - ** ` HttpRequestMessage ` ** — to inspect or log CURL representations before sending
39+ ** Best for:**
40+ Logging, monitoring, or tracing outgoing requests across the application.
1841
19- This is useful for:
20- - Debugging and verifying request payloads or headers
21- - Sharing API calls between teammates
22- - Generating or updating Postman collections easily
42+ ---
43+
44+ ### 💡 Why Use HttpClientToCurl?
45+
46+ - 🧪 Instantly visualize and debug request payloads or headers
47+ - 🤝 Share exact API calls with teammates or QA engineers
48+ - ⚙️ Simplify Postman and CLI reproduction
49+ - 🪶 Lightweight, dependency-free, and easy to integrate
2350
2451---
2552## ⚙️ Installation
@@ -35,7 +62,12 @@ For full examples, detailed usage, and advanced configuration options, please se
3562
3663👉 [ Open Wiki → More Details] ( https://github.com/amingolmahalle/HttpClientToCurlGenerator/wiki )
3764
38- ## 🚀 ** Usage Example**
65+ ---
66+
67+ ## 🚀 Quick Start
68+
69+ ## 🧰 Manual Mode Usage Example
70+
3971``` csharp
4072using System .Text ;
4173using HttpClientToCurl ;
@@ -44,47 +76,115 @@ class Program
4476{
4577 static async Task Main ()
4678 {
79+ var baseAddress = new Uri (" http://localhost:1213/v1/" );
4780 var requestUri = " api/test" ;
4881
49- using var httpClientInstance = new HttpClient ();
50- var baseAddress = new Uri (" http://localhost:1213/v1/" );
51- httpClientInstance .BaseAddress = baseAddress ;
52- string requestBody = /* lang=json,strict*/ @" {"" name"" :"" sara"" ,"" requestId"" :10001001,"" amount"" :20000}" ;
53- HttpRequestMessage httpRequestMessageInstance = new (HttpMethod .Post , requestUri );
82+ using var httpClientInstance = new HttpClient { BaseAddress = baseAddress };
83+
84+ string requestBody = @" {"" name"" :"" sara"" ,"" requestId"" :10001001,"" amount"" :20000}" ;
85+ var httpRequestMessageInstance = new HttpRequestMessage (HttpMethod .Post , requestUri )
86+ {
87+ Content = new StringContent (requestBody , Encoding .UTF8 , " application/json" )
88+ };
5489 httpRequestMessageInstance .Headers .Add (" Authorization" , " Bearer YourAccessToken" );
55- httpRequestMessageInstance .Content = new StringContent (requestBody , Encoding .UTF8 , " application/json" );
5690
57- // Using the HttpClient extension:
91+ // Option 1: Generate curl from HttpClient
5892 httpClientInstance .GenerateCurlInConsole (httpRequestMessageInstance );
5993
60- // Or using the HttpRequestMessage extension:
94+ // Option 2: Generate curl from HttpRequestMessage
6195 httpRequestMessageInstance .GenerateCurlInConsole (baseAddress );
6296
6397 await httpClientInstance .SendAsync (httpRequestMessageInstance );
6498 }
6599}
66100```
67101
68- ## ✅ Output:
69-
102+ ✅ ** Example Output**
70103``` bash
71- curl -X POST ' http://localhost:1213/v1/api/test' -H ' Authorization: Bearer YourAccessToken'
72- -H ' Content-Type: application/json; charset=utf-8' -d ' {"name":"sara","requestId":10001001,"amount":20000}'
104+ curl -X POST ' http://localhost:1213/v1/api/test' \
105+ -H ' Authorization: Bearer YourAccessToken' \
106+ -H ' Content-Type: application/json; charset=utf-8' \
107+ -d ' {"name":"sara","requestId":10001001,"amount":20000}'
73108```
74109
75- ## 🧩 ** Other Features **
110+ ---
76111
77- Works with ** GET ** , ** POST ** , ** PUT ** , ** PATCH ** , and ** DELETE **
112+ ## 🧩 Automatic Mode Usage Example
78113
79- Supports ** JSON ** , ** XML ** , and ** FormUrlEncodedContent **
114+ ### 1️⃣ Global Registration
80115
81- ## ** Output to: **
116+ Enable curl generation globally — every ` HttpClient ` created through ` IHttpClientFactory ` will automatically log curl commands.
82117
83- ** Console** / ** String** / ** File**
118+ ** Program.cs / Startup.cs**
119+ ``` csharp
120+ using HttpClientToCurl ;
121+
122+ // Register global curl generation
123+ builder .Services .AddHttpClientToCurlInGeneralMode (builder .Configuration );
124+
125+ // Register default HttpClient (now curl-enabled)
126+ builder .Services .AddHttpClient ();
127+ ```
128+
129+ ** appsettings.json**
130+ ``` json
131+ "HttpClientToCurl" : {
132+ "TurnOnAll" : true , // Master switch: enable or disable the entire HttpClientToCURL logging system
133+
134+ "ShowOnConsole" : {
135+ "TurnOn" : true , // Enable console output for generated curl commands
136+ "NeedAddDefaultHeaders" : true , // Include default headers (like User-Agent, Accept, etc.) in the curl output
137+ "EnableCompression" : false , // Compress the console log output (not recommended for debugging readability)
138+ "EnableCodeBeautification" : true // Beautify and format the curl command for better readability
139+ },
140+
141+ "SaveToFile" : {
142+ "TurnOn" : true , // Enable saving the generated curl commands into a file
143+ "NeedAddDefaultHeaders" : true , // Include default headers (like User-Agent, Accept, etc.) in the curl output
144+ "EnableCompression" : false , // Compress the saved file (useful if logging a large number of requests)
145+ "Filename" : " curl_commands" , // Name of the output file without extension (e.g., will produce curl_commands.log)
146+ "Path" : " C:\\ Users\\ Public" // Directory path where the log file will be created
147+ }
148+ }
149+ ```
150+
151+ ---
152+
153+ ### 2️⃣ Per-Client Registration
154+
155+ Enable curl logging for specific named clients only.
156+
157+ ** Program.cs / Startup.cs**
158+ ``` csharp
159+ using HttpClientToCurl ;
160+
161+ // Register the curl generator once
162+ builder .Services .AddHttpClientToCurl (builder .Configuration );
163+
164+ // Enable curl logging for selected clients
165+ builder .Services .AddHttpClient (" my-client1" , showCurl : true );
166+ ```
167+ ---
168+
169+ ** appsettings.json**
170+ (same configuration options as above)
171+
172+ ---
173+
174+ ## 🧩 Features
175+
176+ | Feature | Description |
177+ | ----------| --------------|
178+ | 🔁 Methods | Supports ` GET ` , ` POST ` , ` PUT ` , ` PATCH ` , ` DELETE ` |
179+ | 🧠 Content Types | ` JSON ` , ` XML ` , ` FormUrlEncodedContent ` |
180+ | 💾 Output | Console • File • String |
181+ | 🎨 Beautified Output | Optional pretty printing |
182+
183+ ---
84184
85185## 📚 Articles
86186
87- - [ 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/ )
187+ - [ 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/ )
88188- [ 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 )
89189
90190
0 commit comments