White search icon
News
GET request with bearer token to API with Blazor
ASP.NET Core

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.

|

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?

Buy me a coffee

ZetBit on Spotify

An unhandled error has occurred. Reload 🗙

Rejoining the server...

Rejoin failed... trying again in seconds.

Failed to rejoin.
Please retry or reload the page.

The session has been paused by the server.

Failed to resume the session.
Please retry or reload the page.