Copying a curl command out of API documentation and rewriting it as C# is a small task that goes wrong in one predictable place: .NET splits headers between the request and its content. Content-Type belongs to HttpContent, not to HttpRequestMessage, and setting it on the wrong one throws an InvalidOperationException at runtime rather than failing to compile. This converter parses the curl flags — method, headers, body, and the -u flag that becomes Basic auth — and emits an HttpClient program with the headers on the correct object, the body wrapped in a StringContent with the right media type, and the response printed.
Features
Parses method, headers, body and Basic auth from a curl command
Puts Content-Type on the StringContent, where .NET insists it belongs
Renders -u credentials as an AuthenticationHeaderValue
Handles non-standard verbs through the HttpMethod constructor
Correct using directives for whichever features the request needs
Escapes JSON bodies into valid C# string literals
Warns about curl flags that have no client-code equivalent
Download the result as a ready-to-run Program.cs
How to use the cURL to C#
Paste the curl command, including its headers and body
Read the generated C# — the code appears as you type
Copy it, or download Program.cs
Check any warnings about flags that were not translated
Example
Input
curl -X POST https://api.example.com/users -H "Content-Type: application/json" -d '{"name":"Ada"}'
Output
request.Content = new StringContent(
"{\"name\":\"Ada\"}",
Encoding.UTF8,
"application/json");
The media type goes to StringContent, not to request.Headers — the mistake that produces a runtime exception.
Common errors & troubleshooting
Setting Content-Type throws InvalidOperationException. — In .NET, Content-Type is a content header. It has to be set on HttpContent, either through the StringContent constructor or Content.Headers, which is exactly what the generated code does.
The request works in curl but returns 401 in C#. — Check how the credentials arrive. A -u flag becomes a Basic auth header built from a base64 of user:password; a bearer token is an ordinary Authorization header. Both are translated here, but a token pasted with a trailing newline will fail.
HttpClient is created inside a loop and sockets run out. — Reuse a single HttpClient, or inject one from IHttpClientFactory. The generated sample creates one for clarity; in a service, a client per request exhausts sockets under load.
Frequently asked questions
How do I convert a curl command to C#?
Paste it above. The method, URL, headers and body are parsed and emitted as an HttpClient program, with content headers placed on the content object where .NET requires them.
Why can't I set Content-Type on HttpRequestMessage?
Because .NET separates request headers from content headers, and Content-Type is a content header. Assigning it to request.Headers throws at runtime — it belongs on HttpContent.
How do I send Basic authentication in C#?
Base64-encode user:password and set it as an AuthenticationHeaderValue with the Basic scheme on the client's DefaultRequestHeaders. The generated code does exactly that when the curl command uses -u.
Which .NET versions does the generated code target?
Anything with HttpClient and top-level async Main, so .NET Core 3.1 and later including .NET 5 through 9. The using var syntax needs C# 8 or newer.
Is my curl command sent anywhere?
No. It is parsed and converted in your browser, so tokens and API keys inside the command stay on your device.
Related tools
cURL to Fetch — Convert a curl command to a JavaScript fetch() call.
cURL to Python — Convert a curl command to Python requests code.
cURL to Java — Convert a curl command to Java 11 HttpClient code.
cURL to Go — Convert a curl command to Go net/http code.