11# 🥇 HttpClientToCurl
22
3- Generate cURL commands directly from your ` HttpClient ` or ` HttpRequestMessage ` in .NET — perfect for debugging, logging, and sharing HTTP requests.
3+ Generate curl commands directly from your ` HttpClient ` or ` HttpRequestMessage ` in .NET — perfect for debugging, logging, and sharing HTTP requests.
44
55---
66
@@ -14,18 +14,23 @@ Generate cURL commands directly from your `HttpClient` or `HttpRequestMessage` i
1414---
1515
1616## 📖 Overview
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:
1719
18- ** HttpClientToCurl** is a lightweight and powerful .NET extension library that turns your HTTP requests into ** cURL** commands.
20+ ---
21+
22+ ### 🧰 1. Manual Mode
1923
20- You can use its extension methods on:
21- - ** ` HttpClient ` ** — to generate CURL directly when sending requests
22- - ** ` HttpRequestMessage ` ** — to inspect or log CURL representations before sending
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.
2328
2429---
2530
26- ### 🧩 1 . Automatic Mode
31+ ### 🧩 2 . Automatic Mode
2732
28- Automatically generates cURL output whenever your app sends a request.
33+ Automatically generates curl output whenever your app sends a request.
2934You can configure it through dependency injection:
3035
3136- ** Global Registration** — enable for all ` HttpClient ` instances created via ` IHttpClientFactory `
@@ -36,15 +41,6 @@ Logging, monitoring, or tracing outgoing requests across the application.
3641
3742---
3843
39- ### 🧰 2. Manual Mode
40-
41- Generate cURL commands ** on demand** using extension methods on either ` HttpClient ` or ` HttpRequestMessage ` .
42-
43- ** Best for:**
44- Debugging individual requests, creating reproducible Postman calls, or sharing API examples.
45-
46- ---
47-
4844### 💡 Why Use HttpClientToCurl?
4945
5046- 🧪 Instantly visualize and debug request payloads or headers
@@ -53,33 +49,80 @@ Debugging individual requests, creating reproducible Postman calls, or sharing A
5349- 🪶 Lightweight, dependency-free, and easy to integrate
5450
5551---
56-
5752## ⚙️ Installation
5853
59- Install via NuGet:
60-
6154``` bash
6255dotnet add package HttpClientToCurl
6356```
57+ Or visit the NuGet page here: <a href =" https://www.nuget.org/packages/HttpClientToCurl " target =" _blank " >HttpClientToCurl</a >
58+
59+ ## 📚 Documentation
60+
61+ For full examples, detailed usage, and advanced configuration options, please see the ** Wiki** :
6462
65- Or visit the [ NuGet page → ] ( https://www.nuget.org/packages/HttpClientToCurl )
63+ 👉 [ Open Wiki → More Details ] ( https://github.com/amingolmahalle/HttpClientToCurlGenerator/wiki )
6664
6765---
6866
6967## 🚀 Quick Start
7068
71- ### 🔧 1️⃣ Global Registration
69+ ## 🧰 Manual Mode Usage Example
70+
71+ ``` csharp
72+ using System .Text ;
73+ using HttpClientToCurl ;
74+
75+ class Program
76+ {
77+ static async Task Main ()
78+ {
79+ var baseAddress = new Uri (" http://localhost:1213/v1/" );
80+ var requestUri = " api/test" ;
81+
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+ };
89+ httpRequestMessageInstance .Headers .Add (" Authorization" , " Bearer YourAccessToken" );
90+
91+ // Option 1: Generate curl from HttpClient
92+ httpClientInstance .GenerateCurlInConsole (httpRequestMessageInstance );
93+
94+ // Option 2: Generate curl from HttpRequestMessage
95+ httpRequestMessageInstance .GenerateCurlInConsole (baseAddress );
96+
97+ await httpClientInstance .SendAsync (httpRequestMessageInstance );
98+ }
99+ }
100+ ```
101+
102+ ✅ ** Example Output**
103+ ``` bash
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}'
108+ ```
109+
110+ ---
111+
112+ ## 🧩 Automatic Mode Usage Example
72113
73- Enable cURL generation globally — every ` HttpClient ` created through ` IHttpClientFactory ` will automatically log cURL commands.
114+ ### 1️⃣ Global Registration
115+
116+ Enable curl generation globally — every ` HttpClient ` created through ` IHttpClientFactory ` will automatically log curl commands.
74117
75118** Program.cs / Startup.cs**
76119``` csharp
77120using HttpClientToCurl ;
78121
79- // Register global cURL generation
122+ // Register global curl generation
80123builder .Services .AddHttpClientToCurlInGeneralMode (builder .Configuration );
81124
82- // Register default HttpClient (now cURL -enabled)
125+ // Register default HttpClient (now curl -enabled)
83126builder .Services .AddHttpClient ();
84127```
85128
@@ -107,83 +150,41 @@ builder.Services.AddHttpClient();
107150
108151---
109152
110- ### 🔧 2️⃣ Per-Client Registration
153+ ### 2️⃣ Per-Client Registration
111154
112- Enable cURL logging for specific named clients only.
155+ Enable curl logging for specific named clients only.
113156
114157** Program.cs / Startup.cs**
115158``` csharp
116159using HttpClientToCurl ;
117160
118- // Register the cURL generator once
161+ // Register the curl generator once
119162builder .Services .AddHttpClientToCurl (builder .Configuration );
120163
121- // Enable cURL logging for selected clients
164+ // Enable curl logging for selected clients
122165builder .Services .AddHttpClient (" my-client1" , showCurl : true );
123166```
167+ ---
124168
125169** appsettings.json**
126170(same configuration options as above)
127171
128172---
129173
130- ## 🧰 Manual Usage Example
131-
132- ``` csharp
133- using System .Text ;
134- using HttpClientToCurl ;
135-
136- class Program
137- {
138- static async Task Main ()
139- {
140- var baseAddress = new Uri (" http://localhost:1213/v1/" );
141- var requestUri = " api/test" ;
142-
143- using var httpClientInstance = new HttpClient { BaseAddress = baseAddress };
144-
145- string requestBody = @" {"" name"" :"" sara"" ,"" requestId"" :10001001,"" amount"" :20000}" ;
146- var httpRequestMessageInstance = new HttpRequestMessage (HttpMethod .Post , requestUri )
147- {
148- Content = new StringContent (requestBody , Encoding .UTF8 , " application/json" )
149- };
150- httpRequestMessageInstance .Headers .Add (" Authorization" , " Bearer YourAccessToken" );
151-
152- // Option 1: Generate cURL from HttpClient
153- httpClientInstance .GenerateCurlInConsole (httpRequestMessageInstance );
154-
155- // Option 2: Generate cURL from HttpRequestMessage
156- httpRequestMessageInstance .GenerateCurlInConsole (baseAddress );
157-
158- await httpClientInstance .SendAsync (httpRequestMessageInstance );
159- }
160- }
161- ```
162-
163- ✅ ** Example Output**
164- ``` bash
165- curl -X POST ' http://localhost:1213/v1/api/test' \
166- -H ' Authorization: Bearer YourAccessToken' \
167- -H ' Content-Type: application/json; charset=utf-8' \
168- -d ' {"name":"sara","requestId":10001001,"amount":20000}'
169- ```
170-
171- ---
172-
173174## 🧩 Features
174175
175176| Feature | Description |
176177| ----------| --------------|
177178| 🔁 Methods | Supports ` GET ` , ` POST ` , ` PUT ` , ` PATCH ` , ` DELETE ` |
178- | 🧠 Content Types | JSON, XML, ` FormUrlEncodedContent ` |
179+ | 🧠 Content Types | ` JSON ` , ` XML ` , ` FormUrlEncodedContent ` |
179180| 💾 Output | Console • File • String |
180181| 🎨 Beautified Output | Optional pretty printing |
181182
182183---
183184
184185## 📚 Articles
185186
186- - [ 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/ )
187188- [ 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 )
188189
189190
0 commit comments