GET request with bearer token to API with Blazor

GET request with bearer token to API with Blazor

Making a GET request with a bearer token is pretty simple. Just use DefaultRequestHeaders.Authorization in your httpClient instance and insert the bearer token.

14-07-2023 |

Blazor

So today many API's is secured by having some sort of authentication when trying to make a request to them.

When making a GET request in Blazor and actually just in general with C#, you can go and use the DefaultRequestHeaders.Authorization, where you give it a new instance of the AuthenticationHeaderValue. Then specify in the first parameter that it is a "bearer" token you want to insert, and in the next parameter give it the actual bearer token.

In this example there will also be some error handling after the API call.

Index.razor

In this example, the method SendGetRequest will only be runned when clicking the button.


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