Advertisement
Beginner .NET MAUI, Networking & APIs

How do you call a REST API in MAUI?

Short answer

Use HttpClient (ideally via IHttpClientFactory) to send async requests, and System.Text.Json to deserialize responses. Always await calls off the UI thread and handle non-success status codes. Set base address and default headers once on the client.

Save .NET MAUI mock interview
Advertisement
AModel answer
Use HttpClient (ideally via IHttpClientFactory) to send async requests, and System.Text.Json to deserialize responses. Always await calls off the UI thread and handle non-success status codes. Set base address and default headers once on the client.
var response = await _http.GetAsync("products");
response.EnsureSuccessStatusCode();
var products = await response.Content
    .ReadFromJsonAsync<List<Product>>();
Real-world example
A catalog page loads products from a JSON API and binds them to a CollectionView.
Likely follow-ups
  • Why use IHttpClientFactory? | How do you deserialize JSON?
Related topics