Convert curl to C#

Convert curl commands to C# code using HttpClient. curl2code runs entirely in your browser via WebAssembly — your data stays private. For Java development, try our curl to Java converter. For TypeScript, see curl to TypeScript. Below are ready-to-use C# examples.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What is C# HttpClient?

HttpClient is the modern, built-in HTTP client in .NET. It supports async/await natively, HTTP/2, automatic decompression, and connection pooling. Use a single HttpClient instance (or IHttpClientFactory) throughout your application to avoid socket exhaustion. It's the default and only library curl2code uses for C# conversions.

HttpClient vs WebClient vs HttpWebRequest — which to use?

HttpClient is the modern standard — always use it for new .NET projects. WebClient and HttpWebRequest are legacy and should not be used in new code. HttpClient provides async support, better performance, and cleaner API. For similar modern clients, see curl to Java or curl to Go.

How to handle authentication in C#?

Set the header: client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token). For Basic auth, encode credentials and set the header. Per-request auth uses request.Headers.Authorization. curl2code detects -u and -H 'Authorization' and generates the correct C# code.

How to send multipart form data in C#?

Use MultipartFormDataContent: add StringContent for fields and StreamContent with ContentDisposition for files. Pass the multipart content to PostAsync(url, content). The Content-Type boundary is set automatically. curl2code converts -F flags to this pattern.

How to handle HTTP errors in C#?

Call response.EnsureSuccessStatusCode() to throw HttpRequestException on non-2xx status. Or check response.IsSuccessStatusCode and response.StatusCode manually. Wrap in try/catch for HttpRequestException, TaskCanceledException (timeout), and OperationCanceledException.

How to use async/await with HttpClient?

HttpClient is async by design — all methods return Task: var response = await client.GetAsync(url). Use await response.Content.ReadAsStringAsync() for body. For parallel requests, use Task.WhenAll(). Never use .Result or .Wait() — they can cause deadlocks. For similar async patterns, see curl to TypeScript.

How to set timeouts in C# HttpClient?

Set client.Timeout = TimeSpan.FromSeconds(30) for overall timeout. For per-request control, use CancellationTokenSource with a timeout: new CancellationTokenSource(TimeSpan.FromSeconds(10)). For retries, use Polly library or IHttpClientFactory with retry policies.

How to configure a proxy in C#?

Create an HttpClientHandler with proxy: handler.Proxy = new WebProxy("http://proxy:8080"). Pass the handler to new HttpClient(handler). To bypass proxy for local addresses, set handler.Proxy.BypassProxyOnLocal = true. curl2code converts -x and --proxy flags to this setup.

How to send a POST request with JSON body in C#?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to C# using curl2code. The generated code uses new StringContent(json, Encoding.UTF8, 'application/json'). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the HttpClient code.

How to add Bearer token authorization in C#?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates HttpClient code with the auth header: client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue('Bearer', token). curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in C#?

curl2code converts curl -H "Content-Type: application/json" URL to HttpClient code with the proper header: new StringContent(json, Encoding.UTF8, 'application/json'). For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct HttpClient API.

Useful Links

curl Guides

Convert curl to Other Languages