https://github.com/amingolmahalle/httpclienttocurlgenerator
The HttpClientToCurl is a NuGet package for generating curl script of HttpClient in .NET ( C# | CSharp | Dotnet ) supported features: Post, Get, Put, and Delete. content types: application/json, text/xml, application/x-www-form-urlencoded
https://github.com/amingolmahalle/httpclienttocurlgenerator
asp-net asp-net-core aspnetcore codegenerator converter csharp curl dotnet dotnet-standard extension http httpclient json nuget nuget-package package parser postman xml
Last synced: about 1 month ago
JSON representation
The HttpClientToCurl is a NuGet package for generating curl script of HttpClient in .NET ( C# | CSharp | Dotnet ) supported features: Post, Get, Put, and Delete. content types: application/json, text/xml, application/x-www-form-urlencoded
- Host: GitHub
- URL: https://github.com/amingolmahalle/httpclienttocurlgenerator
- Owner: amingolmahalle
- License: mit
- Created: 2022-08-30T09:22:27.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-01-31T09:53:36.000Z (12 months ago)
- Last Synced: 2025-05-16T11:05:41.105Z (8 months ago)
- Topics: asp-net, asp-net-core, aspnetcore, codegenerator, converter, csharp, curl, dotnet, dotnet-standard, extension, http, httpclient, json, nuget, nuget-package, package, parser, postman, xml
- Language: C#
- Homepage: https://www.nuget.org/packages/HttpClientToCurl
- Size: 227 KB
- Stars: 73
- Watchers: 2
- Forks: 13
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ๐ฅ HttpClientToCurl
Generate curl commands directly from your `HttpClient` or `HttpRequestMessage` in .NET โ perfect for debugging, logging, and sharing HTTP requests.
### ๐ Badges
[](https://github.com/amingolmahalle/HttpClientToCurlGenerator/blob/master/LICENSE)
[](https://github.com/amingolmahalle/HttpClientToCurlGenerator/stargazers)
[](https://www.nuget.org/packages/HttpClientToCurl/)
[](https://www.nuget.org/packages/HttpClientToCurl/)

---
## ๐ 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:
---
### ๐งฐ 1. 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.
---
### ๐งฉ 2. 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.
---
### ๐ก Why Use HttpClientToCurl?
- ๐งช Instantly visualise 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
```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**:
๐ [Open Wiki โ More Details](https://github.com/amingolmahalle/HttpClientToCurlGenerator/wiki)
---
## ๐ Quick Start
## ๐งฐ 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
### 1๏ธโฃ Per-Client Registration
Enable curl logging for specific named clients only.
**Program.cs / Startup.cs**
```csharp
using HttpClientToCurl;
// Register the curl generator once
builder.Services.AddHttpClientToCurl(builder.Configuration);
// Enable curl logging for selected clients
builder.Services.AddHttpClient("my-client1").AddCurlLogging();
```
**appsettings.json**
```json
"HttpClientToCurl": {
"Enable": 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
},
"SendToLogger": {
"TurnOn": true, // Enable sending curl commands to ILogger (integrates with Application Insights, Seq, Serilog, etc.)
"NeedAddDefaultHeaders": true, // Include default headers (like User-Agent, Accept, etc.) in the curl output
"EnableCompression": false, // Compress the logged output
"LogLevel": "Debug" // Log level: Trace, Debug, Information, Warning, Error, Critical
}
}
```
---
### 2๏ธโฃ 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
builder.Services.AddAllHttpClientToCurl(builder.Configuration);
// Register default HttpClient (now curl-enabled)
builder.Services.AddHttpClient();
```
**appsettings.json**
(same configuration options as above)
---
## ๐งฉ Features
| Feature | Description |
|----------|--------------|
| ๐ Methods | Supports `GET`, `POST`, `PUT`, `PATCH`, `DELETE` |
| ๐ง Content Types | `JSON`, `XML`, `FormUrlEncodedContent` |
| ๐พ Output | Console โข File โข String โข ILogger |
| ๐จ Beautified Output | Optional pretty printing |
| ๐ Logging Integration | Works with Application Insights, Seq, Serilog, and other ILogger providers |
---
## ๐ 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/)
- [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)
---
## ๐ **Love HttpClientToCurl? Please support us!**
If this project has made your life easier, consider buying us a coffee or sending a donation.
Every bit of support keeps us motivated, helps us add new features, fix bugs, and maintain the project โ keeping it free and awesome for everyone! โ๐
*USDT (Tether โ BEP20 / Binance Smart Chain) wallet address:*
`0x9d03Be8B979453bE300724FD4bb3eF77517d45AE`
---
## ๐ก **Contribute**
Found a bug or want to improve this project?
Open an issue or submit a pull request.
๐ง Contact: amin.golmahalle@gmail.com
## โญ **Give a Star**
If you find this project helpful, please give it a โญ โ it helps others discover it too!
## ๐ **Contributors**