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
-Hbecomesrequest.Headers.TryAddWithoutValidation, which accepts values that the strict parser would reject.Content-Typeis different in .NET: it belongs to the content, so it is set withMediaTypeHeaderValue.Parseonrequest.Content.Headers. - Body:
-d,--data-rawand--jsonbecome aStringContent. Bodies containing quotes or newlines use a verbatim@"..."string with doubled quotes. - Uploads:
-Fbuilds aMultipartFormDataContent; files are added asByteArrayContentfromFile.ReadAllBytes. - Auth:
-usets anAuthenticationHeaderValue("Basic", ...)with a Base64-encoded UTF-8 credential. - Handler options:
--compressedenablesAutomaticDecompression,-ksets theDangerousAcceptAnyServerCertificateValidatorcallback, and-mbecomesclient.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 runIf 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#.