BlazorRest 1.6.1

dotnet add package BlazorRest --version 1.6.1
                    
NuGet\Install-Package BlazorRest -Version 1.6.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="BlazorRest" Version="1.6.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BlazorRest" Version="1.6.1" />
                    
Directory.Packages.props
<PackageReference Include="BlazorRest" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add BlazorRest --version 1.6.1
                    
#r "nuget: BlazorRest, 1.6.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package BlazorRest@1.6.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=BlazorRest&version=1.6.1
                    
Install as a Cake Addin
#tool nuget:?package=BlazorRest&version=1.6.1
                    
Install as a Cake Tool

NuGet Version

BlazorRest

blazor rest is a library for sending http requests in Blazor WebAssembly in the simplest way. blazorrest also gives you the ability to intercept requests, response, and errors that you can customize them. You will no longer have to serialize or deserialize data or even add an auhtorization header.

Installing

To install the package add the following line to you csproj file replacing x.x.x with the latest version number (found at the top of this file

<PackageReference Include="BlazorRest" Version="x.x.x" />

You can also install via the .NET CLI with the following command:

dotnet add package BlazorRest

Setup

In the program.cs file, you can register in the simplest way as follows

builder.Services.AddBlazorRest(opt =>
{
    opt.BaseUri = new Uri("http://localhost:54240");
});

If it is registered above, you can only send http requests without any other functionality But to send Bearer tokens automatically, you can implement the ‍‍‍‍IJwtService interface

public interface IJwtService
{
   ValueTask SetTokenAsync(string? jwtToken);

   ValueTask<string?> GetTokenAsync();

   ValueTask SetRefereshTokenAsync(string? refreshToken);

   ValueTask<string?> GetRefereshTokenAsync();
}

And register the subclass that it has implemented IJwtTokenService interface as follows at the time of registration

builder.Services.AddBlazorRest(opt =>
{
    opt.BaseUri = new Uri("http://localhost:54240");
    opt.UseJwtService<JwtService>();//you can implement IJwtService and use like this
});

implement the ‍‍‍‍‍‍IRequestInterceptor interface To intercept requests.

public interface IRequestInterceptor
{       
   HttpRequestMessage InterceptRequest(HttpRequestMessage request);
}

implement the ‍‍‍‍‍‍IResponseInterceptor interface To intercept response.

public interface IResponseInterceptor
{        
  HttpResponseMessage InterceptResponse(HttpResponseMessage response);
}

implement the ‍‍‍‍‍‍IErrorInterceptor interface For Intercept requests, their status code not equal to 200.

public interface IErrorInterceptor
{ 
   Task InterceptError(ErrorInterceptorModel? error);
}

The most complete way to register blazorrest is as follows.

builder.Services.AddBlazorRest(opt =>
{
    opt.BaseUri = new Uri("http://localhost:54240");
    opt.UseJwtService<JwtService>(); //you can implement IJwtService and use like this
    opt.UseRequestInterceptor<RequestInterceptor>();
    opt.UseResponseInterceptor<ResponseInterceptor>();
    opt.UseErrorInterceptor<ErrorInterceptor>();   
});

sample

login
public class AccountService : IAccountService
{
   private readonly IBlazorRest _blazorRest;
   private readonly IJwtService _jwt;
     
   public AccountService(IJwtService jwt,IBlazorRest blazorRest)
   {
      _jwt = jwt;      
     _blazorRest = blazorRest;
   }

   public async Task Login(UserLoginDto loginDto)
   {
      var message =new BlazorRestMessage("/auth/login", HttpMethod.Post)
      {     
         Content = new BlazorRest.RequestContents.JsonContent(loginDto)   
      };
         
      var result = await _blazorRest.SendAsync<LoginResponse>(message);

      if (!result.IsSuccessful)
      {
         //do somthing
      }    
           
       
      await _jwt.SetTokenAsync(result.Data.Token);           
  }
}
UploadFile
public async Task UploadAvatarAsync(IBrowserFile ProfileImage)
{
   var message = new BlazorRestMessage("/user/avatar", HttpMethod.Post)
   { 
      Content = new FileContent(ProfileImage, "FileNameInFormData")
   };
    
   var result = await _blazorRest.SendAsync(message);
}

upload file with model

public async Task EditUserAsync(EditProfileDto editUserDto, IBrowserFile ProfileImage)
{
    var message = new BlazorRestMessage("/user", HttpMethod.Post)
    {
       Content = new FileWithModelContent(ProfileImage, nameof(ProfileImage), editUserDto)    
    };
    
    var result = await _blazorRest.SendAsync<EditProfileResponse>(message);
    //or
    //var result = await _blazorRest.SendAsync(message);
}

Simple Get Request

public async Task<IEnumerable<WeatherForecast>> Get()
{
    var result = await _blazorRest.GetAsync<WeatherForecast[]>("WeatherForecast");
    
    if (!result.IsSuccessful)
    {
      //do somthing
    }    
      
    return result.Data;    
}

Get Request with Response Options

public async Task<IEnumerable<WeatherForecast>> Get()
{
     var result = await _blazorRest.GetAsync<WeatherForecast[]>("WeatherForecast", new ResponseOptions
            {
                SerializerOptions = new System.Text.Json.JsonSerializerOptions
                {
                    PropertyNameCaseInsensitive = false
                }
            });
    
    if (!result.IsSuccessful)
    {
      //do somthing
    }    
      
    return result.Data;    
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.