curl-to-csharp icon indicating copy to clipboard operation
curl-to-csharp copied to clipboard

curl to C# converter

curl to C#

Build status Quality Gate Docker pulls

This ASP.NET Core app converts curl commands to C# code

Demo

https://curl.olsh.me

Telegram bot

https://t.me/curl_to_csharp_bot

Run with docker

  1. Run container
docker run -p 8080:80 olsh/curl-to-csharp
  1. Open http://localhost:8080

Run locally

You can grab latest binaries here and run dotnet CurlToCSharp.dll

Build

  1. Install cake
dotnet tool install -g Cake.Tool
  1. Run build
dotnet cake build.cake

NuGet Packages

Curl.CommandLine.Parser

NuGet

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

NuGet

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);
    }
}
*/