One typical need in Blazor apps is to make HTTP calls. Think of AJAX-style (XMLHttpRequest or fetch) operations, which are the bread and butter of SPAs. For that, we need an HTTP client, and the most convenient one is HttpClient.
We first need to register the services for it in the ConfigureServices method (for the Server hosting model), as follows:
services.AddHttpClient();
Then, we can inject the IHttpClientFactory service in our Blazor app, and from it build HttpClient, as illustrated in the following code snippet:
[Inject]
public IHttpClientFactory HttpClientFactory { get; set; }
HttpClient HttpClient => HttpClientFactory.CreateClient();
There are different overloads to AddHttpClient, for when we need to configure a named client with specific settings—default headers, timeouts—and then create that client in CreateClient, but I won't go through that here.
HttpClient can send POST, GET, PUT...