Skip to the content.

CurlDotNet Cookbook: curl recipes in C#

These curl recipes in C# show how to do one common HTTP task each with CurlDotNet, the pure .NET library that runs curl command strings. Every recipe gives the curl command first, the Curl.ExecuteAsync call that runs it, and the CurlRequestBuilder equivalent where one exists.

Installation

dotnet add package CurlDotNet

Package page: CurlDotNet on NuGet.

Beginner recipes

Start here if you are new to CurlDotNet. See also the beginner overview.

One flag at a time

Quick examples

GET

using CurlDotNet;

var result = await Curl.ExecuteAsync("curl https://api.github.com/zen");   // sends User-Agent: curl/8.7.1, like curl
Console.WriteLine(result.Body);

POST JSON

using CurlDotNet;

var result = await Curl.ExecuteAsync(@"
    curl -X POST https://httpbin.org/post \
         -H 'Content-Type: application/json' \
         -d '{""name"":""Ada""}'");

Console.WriteLine(result.IsSuccess ? "created" : $"HTTP {result.StatusCode}");

Download a file

using CurlDotNet;

var result = await Curl.ExecuteAsync("curl -L -o image.png https://httpbin.org/image/png");
Console.WriteLine(result.OutputFiles[0]);

Find a recipe by task

I want to… Recipe
Fetch data from a URL curl GET request
Send JSON to an API curl -X POST JSON
Submit an HTML-style form curl -d form data
Upload a file curl -F file upload
Save a response to disk curl download file
Update or delete a resource PUT, DELETE and PATCH
Log in with a username and password digest, NTLM and basic auth
Send a session cookie cookies
Go through a corporate proxy proxy
Stop a slow request timeout
Check headers without downloading HEAD request
Follow a 301/302 follow redirects
Deal with failures handle errors
Retry a flaky endpoint retry
Print the status code or timing write-out
Fetch a numbered series of URLs multiple URLs and globbing
Keep options or credentials in a file config files, variables and .netrc

How the recipes are written

Each page states the problem, shows the curl command and the C# that runs it, then the builder version, a few variations, the things that commonly go wrong, and links onward. Every snippet is short enough to paste into a console app with using CurlDotNet; (and using CurlDotNet.Core; for the builder and CurlResult).

More documentation

Questions and bug reports: GitHub Issues.