Skip to the content.

How to use curl in Blazor and .NET MAUI

To use curl in Blazor or .NET MAUI, put your CurlDotNet calls in a small service class, register it in Program.cs (Blazor) or MauiProgram.cs (MAUI), and inject it into components and pages. The CurlDotNet API is the same everywhere; what differs is where the code runs.

dotnet add package CurlDotNet

A shared service

using CurlDotNet;

public class QuoteService
{
    public async Task<string> GetRawAsync(CancellationToken ct = default)
    {
        var result = await Curl.ExecuteAsync("curl -m 10 https://httpbin.org/json", ct);
        return result.IsSuccess ? result.Body : $"Error {result.StatusCode}";
    }
}

The fluent builder works the same way:

using CurlDotNet.Core;

var result = await CurlRequestBuilder
    .Get("https://httpbin.org/json")
    .WithTimeout(TimeSpan.FromSeconds(10))
    .ExecuteAsync();

Register it with builder.Services.AddSingleton<QuoteService>(); in either host.

Use it from a Razor component

@inject QuoteService Quotes

<button @onclick="Load">Load</button>
<pre>@json</pre>

@code {
    private string? json;
    private async Task Load() => json = await Quotes.GetRawAsync();
}

The component is the same in Blazor Server and in a MAUI Blazor Hybrid app. Blazor WebAssembly has extra limits, described below.

Blazor Server

The code runs on your server, so everything on the other how-to pages applies: proxies, -k, file output with -o, any URL your server can reach. Treat it like ASP.NET Core.

Blazor WebAssembly: call through your server

In WebAssembly the code runs in the browser, where .NET’s HttpClient is built on the browser’s fetch. That brings browser rules:

CurlDotNet has not been tested in Blazor WebAssembly for this page, and creating that handler is expected to throw PlatformNotSupportedException there. The dependable pattern is to run CurlDotNet in your server API (ASP.NET Core) and have the WebAssembly client call that API with the injected HttpClient.

.NET MAUI

MAUI apps run on .NET 8+ on Android, iOS, macOS and Windows, and CurlDotNet’s net8.0 and later builds are pure managed code, so no native library has to ship with the app. Points to check:

The MAUI notes are platform guidance; CurlDotNet was not run on an Android or iOS device for this page.