curl to C#
Convert curl commands to C# HttpClient code for .NET 6+.
Load Preset Sample:
curl to C# HttpClient Converter
This tool converts curl commands into C# code using HttpClient — the standard .NET HTTP client available in .NET 6 and later. The generated code uses HttpRequestMessage for full control over headers and request bodies, and StringContent for JSON payloads. Basic auth via -u user:pass is converted to an AuthenticationHeaderValue. Supports common curl flags: -X, -H, -d, -u, and -k.
Built and maintained by Meet Shah · Last updated
What this tool is used for
- Converting an API's curl example into HttpClient code for .NET.
- Reproducing a captured request inside an existing C# client.
- Getting the content and header setup right for a POST.
- Producing a snippet for a console tool or a minimal API handler.
- Checking how auth headers attach to a request rather than a client.
Frequently Asked Questions
- Why should I not wrap HttpClient in a using block?
- Disposing HttpClient per request leaves sockets in TIME_WAIT and exhausts the connection pool under load — the well-documented socket exhaustion problem. Use a single static instance, or register IHttpClientFactory and inject typed clients.
- What is the default timeout?
- 100 seconds, set on the HttpClient instance rather than per request. For finer control pass a CancellationToken to SendAsync. Note the timeout applies to the whole operation including reading the body, not just the connect.
- How do I send a JSON body?
- new StringContent(json, Encoding.UTF8, "application/json"), or in .NET 5+ the simpler PostAsJsonAsync extension from System.Net.Http.Json, which serialises and sets the content type in one call.
- Does HttpClient throw on a 404?
- Not by default — inspect response.IsSuccessStatusCode, or call response.EnsureSuccessStatusCode() to convert a non-2xx into an HttpRequestException. Silently ignoring the status is a frequent porting bug.
- How does curl's -k translate?
- Supply an HttpClientHandler with ServerCertificateCustomValidationCallback set to return true. Scope it to a dedicated client — applying it globally disables certificate validation for every call your app makes.
- Why does a long-lived HttpClient miss DNS changes?
- Because it keeps connections open indefinitely and never re-resolves. Setting `PooledConnectionLifetime` on a `SocketsHttpHandler` recycles them on a schedule, which is the fix for a client that keeps talking to a decommissioned IP.
- What does IHttpClientFactory solve?
- It manages handler lifetime for you, avoiding both socket exhaustion from per-request clients and the stale-DNS problem from a static one. In an ASP.NET Core app it is the recommended way to get a client.
- Should I use System.Text.Json or Newtonsoft?
- System.Text.Json is built in, faster and the default for new code; Newtonsoft is more permissive and still needed for some features. The generated code targets the built-in one, which is stricter about case and trailing commas.
Common errors and gotchas
- Creating and disposing an HttpClient per request, which exhausts sockets — reuse one or use the factory.
- Setting a content-type on the client's default headers rather than on the content, which throws.
- Calling `.Result` on an async call, which deadlocks in some contexts.
- Assuming a non-2xx throws, when you must call `EnsureSuccessStatusCode` yourself.
- Omitting a timeout, since the default of 100 seconds is often far too long.
Related Developer Utilities tools
RegExp Tester
Test regular expressions and inspect matches locally.
Regex Visualizer
Visual regex pattern diagram with live match highlighting and capture group annotations.
Subnet Calculator
Compute CIDR subnets, usable hosts, and network ranges.
Cron Parser
Translate cron syntax into plain English.
URL Parser
Break a URL into protocol, host, path, and query parts.
HTML Previewer
Paste HTML and see it rendered live in a safe, sandboxed preview.
HTTP Status Code Reference
Search and look up every HTTP status code and its meaning.
MIME Type Lookup
Find the MIME type for a file extension, or the extensions for a MIME type.