CurlDotNet
CurlDotNet
Curl Class
🚀 THE MAIN CURLDOTNET CLASS - Start here!
This class lets you run ANY curl command in C# by just copying and pasting it as a string. No translation needed. No learning curve. If it works in curl, it works here.
Quick Start:
// Just paste any curl command as a string:
var response = await Curl.Execute("curl https://api.github.com");
Console.WriteLine(response.Body); // That's it! You're done!
What is curl? curl is the universal tool for making HTTP requests from the command line. Every API documentation uses it. Now you can use those exact same commands in your C# code.
Learn more:
- 📖 curl documentation: https://curl.se/docs/
- 📚 curl tutorial: https://curl.se/docs/tutorial.html
- ⌨️ curl command generator: https://curlbuilder.com/
public static class Curl
Inheritance System.Object 🡒 Curl
Remarks
Why use CurlDotNet instead of HttpClient?
- ✂️ Copy & Paste - Use commands directly from API docs without translation
- 🎓 No Learning Curve - If you know curl (everyone does), you know this
- 🔄 Easy Migration - Move from bash scripts to C# without rewriting
- 📦 All Features - Supports all 300+ curl options out of the box
Thread Safety: All methods are thread-safe. You can call them from multiple threads simultaneously.
Memory Efficiency: Responses are streamed, not loaded into memory all at once. Perfect for large files.
Sponsored byIronSoftware - creators of IronPDF, IronOCR, IronXL, and IronBarcode.
Two Ways to Use CurlDotNet:
- Paste curl commands: Just copy/paste any curl command string - it works!
var result = await Curl.ExecuteAsync("curl -X POST https://api.example.com/data -H 'Content-Type: application/json' -d '{\"key\":\"value\"}'");
- Use fluent builder: For programmatic API with IntelliSense
var result = await CurlRequestBuilder.Post("https://api.example.com/data").WithHeader("Content-Type", "application/json").WithJson(new { key = "value" }).ExecuteAsync();
| Properties | |
|---|---|
| DefaultConnectTimeoutSeconds | Sets how long to wait for a connection to be established (in seconds). This is different from the total timeout - it only applies to making the initial connection. Like adding --connect-timeout to every curl command. Example:`...` Tip: Set this lower than DefaultMaxTimeSeconds to fail fast on dead servers. Learn more:curl --connect-timeout documentation |
| DefaultFollowRedirects | Controls whether curl automatically follows HTTP redirects (301, 302, etc). When true, acts like adding -L or --location to every command. Many APIs use redirects, so you often want this enabled. Example:`...` Security note: Be careful following redirects to untrusted sources. Learn more:curl -L documentation |
| DefaultInsecure | ⚠️ WARNING: Disables SSL certificate validation - ONLY use for development/testing! When true, acts like adding -k or --insecure to every command. This accepts any SSL certificate, even self-signed or expired ones. Example (DEVELOPMENT ONLY):`...` 🔴 NEVER use this in production! It makes you vulnerable to man-in-the-middle attacks. Learn more:curl -k documentation |
| DefaultMaxTimeSeconds | Sets a global timeout for all curl operations (in seconds). This is like adding --max-time to every curl command automatically. Set to 0 (default) for no timeout. Individual commands can still override this. Example:`...` Learn more:curl --max-time documentation |
| Methods | |
|---|---|
| Download(string, string) | ⚠️ SYNCHRONOUS file download (blocks thread). |
| DownloadAsync(string, string) | Download a file from a URL and save it to disk. Downloads any file and saves it to the specified path. Shows progress if the file is large. Example:`...` With Error Handling:`...` |
| Execute(string) | ⚠️ SYNCHRONOUS - Execute curl command and WAIT for it to complete (BLOCKS thread). This method BLOCKS your thread until the HTTP request completes. Your application will FREEZE during this time. Only use when async is not possible. When to use SYNC (this method):... Example - When sync is OK:`...` ⚠️ WARNING: This blocks your thread. The application cannot do anything else while waiting for the HTTP response. Use ExecuteAsync instead whenever possible! |
| Execute(string, CurlSettings) | ⚠️ SYNCHRONOUS with settings - Blocks thread with advanced options. |
| Execute(string, CancellationToken) | ⚠️ SYNCHRONOUS with cancellation - Blocks thread but can be cancelled. Still BLOCKS your thread, but can be cancelled. Prefer ExecuteAsync with cancellation.`...` |
| ExecuteAsync(string) | 🎯 THE MAIN METHOD - Executes any curl command and returns the response. Just paste ANY curl command as a string. It works exactly like running curl from the command line, but returns the result as a nice C# object you can work with. Simple Example:`...` Real-World Example from Stripe Docs:`...` All HTTP Methods Supported:`...` Common Options:`...` |
| ExecuteAsync(string, CurlSettings) | Execute with advanced settings - for when you need more control. Use this overload when you need features beyond what curl command strings provide, like progress callbacks, custom HTTP handlers, or retry policies. Example with Progress Reporting:`...` Example with Custom Retry Policy:`...` |
| ExecuteAsync(string, CancellationToken) | Execute a curl command with cancellation support - perfect for long-running operations. This lets you cancel the HTTP request if it's taking too long or if the user cancels. Essential for good user experience in desktop and mobile apps. Basic Example:`...` User-Cancellable Download:`...` Web API with Request Timeout:`...` |
| ExecuteManyAsync(string[]) | Execute multiple curl commands in parallel - great for performance! Runs multiple HTTP requests at the same time, which is much faster than running them one by one. Perfect for fetching data from multiple APIs or endpoints simultaneously. Example - Fetch Multiple APIs:`...` Example - Aggregate Data:`...` Error Handling - Some May Fail:`...` |
| Get(string) | ⚠️ SYNCHRONOUS GET request (blocks thread).`...` |
| GetAsync(string) | Quick GET request - simpler syntax for basic GET operations. When you just need to GET a URL without any options, use this shortcut method. Example:`...` |
| Post(string, string) | ⚠️ SYNCHRONOUS POST request (blocks thread). |
| PostAsync(string, string) | Quick POST request - simpler syntax for posting data. Convenient method for simple POST requests with string data. Example:`...` |
| PostJson(string, object) | ⚠️ SYNCHRONOUS POST with JSON (blocks thread). |
| PostJsonAsync(string, object) | POST with JSON data - automatically serializes objects to JSON. The easiest way to POST JSON data. Pass any object and it's automatically serialized to JSON with the correct Content-Type header. Example:`...` Works with any object:`...` |
| ToFetch(string) | Convert curl command to JavaScript fetch() code. Generates JavaScript code that does the same thing as your curl command. Useful for web developers who need the same request in JavaScript. Example:`...` |
| ToHttpClient(string) | Convert curl command to C# HttpClient code - great for learning! Shows you exactly how to write the same request using HttpClient. Perfect for understanding what curl is doing or migrating to pure HttpClient. Example:`...` |
| ToPythonRequests(string) | Convert curl command to Python requests code. Generates Python code using the popular 'requests' library. Great for Python developers or data scientists. Example:`...` |
| Validate(string) | Check if a curl command is valid without executing it. Useful for validating user input or checking commands before running them. This only checks syntax, not whether the URL actually exists. Example:`...` Validate User Input:`...` |