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.
- curl GET request in C# - fetch a URL, check the status, read headers
- curl -X POST JSON in C# -
-d,--json,WithJson,ParseJson<T>() - curl -d form data POST in C# - URL-encoded forms and
--data-urlencode - curl -F file upload in C# - multipart/form-data,
-Tuploads and--data-binary - curl download file in C# (curl -o) -
-o,-O,-J,-C -resume,Curl.DownloadAsync - Handle curl errors and exceptions in C# - status codes,
-f,EnsureSuccess(), typed exceptions and exit codes - Call a REST API with curl in C# - bearer tokens, verbs, a reusable client
One flag at a time
- curl follow redirects in C# (curl -L) -
-L,--max-redirs,CurlTooManyRedirectsException - curl with cookies in C# (curl -b and -c) - cookie strings, cookie files and
-ccookie jars - curl proxy in C# (curl -x and SOCKS5) -
-x,--proxy-user,--noproxy, proxy environment variables,--socks5 - curl timeout in C# (–max-time and –connect-timeout) -
-m,--connect-timeout, no hidden default,CurlOperationTimeoutException - curl -I HEAD request in C# - headers without the body
- curl PUT, DELETE and PATCH requests in C# -
-Xwith and without a body - curl digest, NTLM and basic auth in C# -
-u,--digest,--ntlm,--anyauth,.netrc, bearer tokens - curl –retry in C# -
--retry,--retry-delay,--retry-max-time,WithRetries - curl -w write-out in C# -
%{http_code},%{time_total},%{json}, output on failure - curl multiple URLs and URL globbing in C# - several URLs,
{a,b},[1-10],#1,--next - curl config files, –variable and .netrc in C# -
-K,--variable, ``,-n
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
- Tutorials - a step-by-step learning path
- Getting started - installation and your first request
- API reference - generated from the source
- Troubleshooting - common issues and fixes
- Exceptions - every exception type and when it is thrown
- curl exit codes - every exit code and the exception it maps to
Questions and bug reports: GitHub Issues.