curl to C#
This ASP.NET Core app converts curl commands to C# code
Demo
Telegram bot
https://t.me/curl_to_csharp_bot
Run with docker
- Run container
docker run -p 8080:80 olsh/curl-to-csharp
Run locally
You can grab latest binaries here and run dotnet CurlToCSharp.dll
Build
- Install cake
dotnet tool install -g Cake.Tool
- Run build
dotnet cake build.cake
NuGet Packages
Curl.CommandLine.Parser
Key Features
- Parses cURL command into individual cURL options.
- Returns parsing errors and warnings if the cURL input is invalid.
Installation
Install with NuGet
dotnet add package Curl.CommandLine.Parser
Usage/Examples
var input = @"curl https://sentry.io/api/0/projects/1/groups/?status=unresolved -d '{""status"": ""resolved""}' -H 'Content-Type: application/json' -u 'username:password' -H 'Accept: application/json' -H 'User-Agent: curl/7.60.0'";
var output = new CurlParser(new ParsingOptions() { MaxUploadFiles = 10 }).Parse(input);
Console.WriteLine(output.Data.UploadData.First().Content);
// Output:
// {"status": "resolved"}
Curl.HttpClient.Converter
Key Features
- Converts output from CurlParser into C# code.
- Returns parsing errors and warnings if the cURL input is invalid.
Installation
Install with NuGet
dotnet add package Curl.HttpClient.Converter
Usage/Examples
var input = @"curl https://sentry.io/api/0/projects/1/groups/?status=unresolved -d '{""status"": ""resolved""}' -H 'Content-Type: application/json' -u 'username:password' -H 'Accept: application/json' -H 'User-Agent: curl/7.60.0'";
var curlOption = new CurlParser().Parse(input);
var output = new CurlHttpClientConverter().ToCsharp(curlOption.Data);
// Output:
/*
// In production code, don't destroy the HttpClient through using, but better reuse an existing instance
// https://www.aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://sentry.io/api/0/projects/1/groups/?status=unresolved"))
{
request.Headers.TryAddWithoutValidation("Accept", "application/json");
request.Headers.TryAddWithoutValidation("User-Agent", "curl/7.60.0");
var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");
request.Content = new StringContent("{\"status\": \"resolved\"}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var response = await httpClient.SendAsync(request);
}
}
*/