Make GET request to API with Blazor

Make GET request to API with Blazor

Original YouTube Logo

Article have related YouTube video

When you know how to create a GET request to an API, it is actually not that hard to do, and with Blazor it is even more simple to display the data.

13-07-2023 |

Blazor

In this example we will use the test API from jsonplaceholder.typicode.com. Here we get a simple set of data when making a request to the url: https://jsonplaceholder.typicode.com/todos/1

We will get a response that will give us a userId, id, title and completed.

In the following example you can see at the bottom of the code section, that i create a class called Todo to actually handle the object that we get in return. So go an use Postman to see what response you get back, and then go and make the object so the httpclient.GetFromJsonAsync knows what to expect in return so you easily can save it in a variable that can contain that object.

This is the code you could just go, copy and paste into your file. In this case it is created in the Index.razor file.

Index.razor

It is pretty much just a button where when you click it, it will make the request and save the response into the todo variable. When the todo variable is not null anymore, it will display (with the help of blazor) the data that we get from the request.

I hope it helped you to make a simple GET request with Blazor.


Like what you see?


New C# Learning Area - Free!

Our website now offers a new section on C# theory, including code examples and related video tutorials.

ZetBit

Subscribe to ZetBits Newsletter