cURL → C#
.NET API Code Generator

cURL to C# Converter (.NET HttpClient, RestSharp & Flurl)

Translate command-line cURL requests into idiomatic C# code using .NET HttpClient, RestSharp, or Flurl.Http.

100% Client-Side Privacy
Zero Server Transmission
Presets:
cURL Command Input
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;

using var client = new HttpClient();

using var request = new HttpRequestMessage(HttpMethod.Post, "https://api.openai.com/v1/chat/completions");

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "sk-proj-mockToken998877665544332211");

var jsonPayload = """
{
  "model": "gpt-4o",
  "messages": [
    {
      "role": "system",
      "content": "You are a professional C# and .NET full-stack architect."
    },
    {
      "role": "user",
      "content": "Demonstrate modern HttpClient and RestSharp best practices."
    }
  ],
  "temperature": 0.7,
  "max_tokens": 1000
}
""";
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

try
{
    using var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
    var responseBody = await response.Content.ReadAsStringAsync();
    Console.WriteLine(responseBody);
}
catch (HttpRequestException ex)
{
    Console.WriteLine($"[HTTP Request Failed] {ex.StatusCode}: {ex.Message}");
}
HTTP Method & Protocol
POSTHTTPS
Target Host & Route
https://api.openai.com
0 query params
Headers & Payload
2 headers • BEARER Auth
JSON Payload (260 B)

100% Client-Side Transpilation

cURL syntax parsing, argument tokenization, and C# code generation run entirely inside your browser memory without server network round-trips.

.NET 8/9 & Multi-Library Dialects

Generate modern HttpClient, RestSharp v107+, Flurl.Http fluent requests, or ASP.NET Core IHttpClientFactory dependency injection patterns.

Complete Flag & Auth Parity

Safely transpiles Bearer tokens, Basic Auth (-u), custom headers (-H), multipart form-data (-F), timeouts (-m), insecure SSL (-k), and redirects (-L).

Technical Reference & Architecture Guide

cURL to C# Converter (.NET HttpClient, RestSharp & Flurl) Developer Guide — Reference Guide

Translate command-line cURL requests into idiomatic C# code using .NET HttpClient, RestSharp, or Flurl.Http with asynchronous patterns and connection pooling best practices.

Key Parameters & Controls

targetLibrarystring

Target C# HTTP client library: Modern .NET HttpClient, RestSharp, or Flurl.Http.

Default: HttpClient
asyncPatternboolean

Generates modern async/await method signatures with CancellationToken parameters.

Default: true
jsonHandlingstring

JSON serialization engine used for request payload and response body parsing (System.Text.Json vs Newtonsoft.Json).

Default: System.Text.Json
instantiationStylestring

Specifies whether HttpClient is created locally or injected through IHttpClientFactory to avoid socket exhaustion.

Default: IHttpClientFactory

Pro Tips & Best Practices

Prevent Socket Starvation with IHttpClientFactory

Never wrap HttpClient in a using statement inside high-throughput loops. Each disposal leaves TCP sockets in a TIME_WAIT state, depleting available OS ports. Register clients via IHttpClientFactory or reuse a shared singleton instance.

Stream Directly with System.Net.Http.Json

Instead of allocating intermediate JSON strings with JsonConvert.SerializeObject, use PostAsJsonAsync<T>() and ReadFromJsonAsync<T>(). These methods stream payloads directly to and from memory buffers, reducing garbage collection pressure.

Rotate DNS Records using SocketsHttpHandler

When using singleton HttpClient instances, configure SocketsHttpHandler.PooledConnectionLifetime to TimeSpan.FromMinutes(15). This forces the handler to recreate connections periodically, ensuring DNS updates and failover targets are recognized.

Handle Flurl FlurlHttpException Gracefully

When adopting Flurl.Http, catch FlurlHttpException rather than generic HttpRequestException. This gives immediate access to ex.StatusCode and ex.GetResponseStringAsync() for comprehensive error diagnostics.

Common Gotchas & Troubleshooting

HttpRequestException: The SSL connection could not be established

This error occurs when targeting self-signed certificates or unsupported TLS ciphers (equivalent to curl -k). In development environments, configure DangerousAcceptAnyServerCertificateValidator on HttpClientHandler, but ensure strict certificate validation in production.

SocketException: Only one usage of each socket address is normally permitted

Caused by socket exhaustion from repeated new HttpClient() instantiations. Refactor to use IHttpClientFactory dependency injection or a static/singleton HttpClient instance.

415 Unsupported Media Type error on JSON POST requests

Verify that your payload is wrapped with a StringContent specifying Encoding.UTF8 and media type application/json, or use PostAsJsonAsync which sets the Content-Type header automatically.

Frequently Asked Questions

How does cURL Basic Authentication (-u user:password) translate to C# HttpClient?▼

Credentials are Base64-encoded and attached to the request authorization header via client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("user:password")));.

Where should custom request headers (-H) be added in .NET HttpClient?▼

Headers can be added globally via client.DefaultRequestHeaders or per request using HttpRequestMessage.Headers.Add(). Note that content-specific headers such as Content-Type must be configured on HttpContent.Headers instead.

What is the difference between HttpClient, RestSharp, and Flurl.Http?▼

HttpClient is the built-in, highly optimized standard in .NET. RestSharp offers an intuitive request/response model suited for legacy codebases and rapid integration. Flurl.Http provides a fluent, chainable URL builder with built-in test mocking and JSON serialization.

How does cURL multipart form-data (-F key=value -F file=@path) map to C#?▼

It translates to MultipartFormDataContent. Text fields use StringContent, while file uploads use ByteArrayContent or StreamContent with fileName and Content-Type explicitly assigned.

Does this conversion tool send my cURL request or API keys to any backend server?▼

No. All cURL token parsing, AST derivation, and C# code synthesis execute entirely within your client browser session. No data or credentials are ever transmitted to an external server.

Related Tools & Utilities

All Tools →