Skip to content

olsh/curl-to-csharp

master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
src
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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