FormatterHQ

cURL to C# Converter

Paste a curl command and get C# code that sends the same request with HttpClient and HttpRequestMessage, ready for a .NET console app.

Input SHELL
Output CSHARP
Ready.

How to use the cURL to C# Converter

  1. Paste your curl command into the input area.
  2. The C# code is generated automatically; click Convert after editing.
  3. Create a console app with dotnet new console and replace Program.cs with the output.
  4. Run it with dotnet run and review any comments about unconverted flags.

Convert curl to C# HttpClient code

This curl to C# converter writes .NET code that reproduces your curl request with HttpClient, HttpClientHandler and an explicit HttpRequestMessage. The output uses top-level statements and await, so it drops straight into the Program.cs of a new console project on .NET 6 or later.

Example

Here is the result for curl -k -u admin:s3cret --compressed -L https://intranet.example.com/api/reports:

var handler = new HttpClientHandler();handler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;handler.AutomaticDecompression = System.Net.DecompressionMethods.All;using var client = new HttpClient(handler);using var request = new HttpRequestMessage(new HttpMethod("GET"), "https://intranet.example.com/api/reports");request.Headers.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("admin:s3cret")));using var response = await client.SendAsync(request);

Because -L was present, redirects stay on. Without it, the converter adds handler.AllowAutoRedirect = false;, since .NET follows redirects by default and curl does not.

How options are translated

  • Headers: each -H becomes request.Headers.TryAddWithoutValidation, which accepts values that the strict parser would reject. Content-Type is different in .NET: it belongs to the content, so it is set with MediaTypeHeaderValue.Parse on request.Content.Headers.
  • Body: -d, --data-raw and --json become a StringContent. Bodies containing quotes or newlines use a verbatim @"..." string with doubled quotes.
  • Uploads: -F builds a MultipartFormDataContent; files are added as ByteArrayContent from File.ReadAllBytes.
  • Auth: -u sets an AuthenticationHeaderValue("Basic", ...) with a Base64-encoded UTF-8 credential.
  • Handler options: --compressed enables AutomaticDecompression, -k sets the DangerousAcceptAnyServerCertificateValidator callback, and -m becomes client.Timeout.

Limitations and tips

Proxy (-x) and client certificate (--cert) settings are not converted; add them to the handler yourself. The sample creates a new HttpClient per run, which is fine for a script. In a long-running app, reuse one instance or use IHttpClientFactory to avoid socket exhaustion.

Keep -k for local testing only. Accepting any certificate disables TLS protection against interception.

The code prints the numeric status and the body as a string. For JSON APIs, call response.EnsureSuccessStatusCode() first so that 4xx and 5xx responses raise an HttpRequestException instead of being parsed as data.

Running the generated code

The output relies on top-level statements and using var declarations, so it needs .NET 6 or later. The quickest way to try it:

dotnet new console -n CurlTest
cd CurlTest
# replace the contents of Program.cs with the converted code
dotnet run

If the server answers differently than it did to curl, compare the two requests header by header. Run the original command with curl -v to print what it sent, and remember that .NET adds no User-Agent header by default, while curl sends curl/ plus its version. Some APIs and firewalls reject requests without one; add it with client.DefaultRequestHeaders.UserAgent.ParseAdd("MyApp/1.0").

Privacy

The conversion runs locally in your browser; passwords and bearer tokens in the command are never sent anywhere. You can also convert the same command with curl to Java or curl to JavaScript, and create response classes with JSON to C#.

Frequently asked questions

Why is Content-Type not added with request.Headers?
In .NET, Content-Type is a content header, and adding it to the request headers throws an exception. The converter therefore sets it on request.Content.Headers.ContentType.
Does the code work with .NET Framework?
The HttpClient calls exist there too, but the output uses top-level statements and using var, which need a newer C# version. On .NET Framework, wrap the code in an async Task Main method and replace DecompressionMethods.All with GZip | Deflate.
How do I deserialize the JSON response?
Use await response.Content.ReadFromJsonAsync<MyType>() from System.Net.Http.Json, or pass the string to JsonSerializer.Deserialize. Generate MyType from a sample response with the JSON to C# tool.
Should I create a new HttpClient for every request?
No, not in a long-running service. Create one and reuse it, or register it through IHttpClientFactory, so connections are pooled and sockets are not exhausted.
Is it safe to keep the -k certificate bypass?
Only against a local or test server with a self-signed certificate. In production it lets anyone intercept the traffic, so remove the callback and install a trusted certificate instead.

Last updated October 3, 2026

Related tools

cURL to Axios (Node.js) Converter Convert curl commands to Node.js axios requests with params, headers, JSON data, auth, form-data uploads and timeouts. cURL to Go Converter Convert curl commands into Go code using net/http — method, headers, JSON or form body, basic auth, timeouts and multipart uploads. cURL to PHP Guzzle Converter Convert curl commands to PHP Guzzle requests using json, form_params, multipart, query and auth options. cURL to Java Converter Convert curl commands to Java 11+ java.net.http.HttpClient code with headers, body, redirects and timeouts. cURL to JavaScript Fetch Convert curl commands into JavaScript fetch() code with method, headers, JSON body, form data, basic auth and timeouts. cURL to PHP Converter Convert curl commands into PHP code using the curl_* functions: method, headers, body, auth, cookies and SSL options. cURL to Python Requests Convert curl commands into Python requests code: headers, params, JSON or form data, files, cookies, auth and timeouts. cURL to Rust Converter Convert curl commands to Rust using the reqwest crate, with JSON bodies via serde_json, headers, auth and timeouts. Date & Time Calculator Calculate the difference between two dates or add/subtract days, weeks, months and years from a date. Docker Run to Docker Compose Convert one or more docker run commands into a docker-compose.yml: ports, volumes, env, restart, networks, healthchecks and more. File Type Checker Find out what a file really is from its content, not its name. Detects 150+ formats and warns when the extension does not match. HTTP Status Codes Searchable list of the HTTP status codes in use, from 100 to 511, with a plain explanation of when each response is used.