{"id":20409380,"url":"https://github.com/jsakamoto/toolbelt.blazor.httpclientinterceptor","last_synced_at":"2025-08-19T18:36:17.265Z","repository":{"id":42501808,"uuid":"148791624","full_name":"jsakamoto/Toolbelt.Blazor.HttpClientInterceptor","owner":"jsakamoto","description":"Intercept all of the sending HTTP requests on a client side Blazor application.","archived":false,"fork":false,"pushed_at":"2024-07-30T14:02:02.000Z","size":385,"stargazers_count":75,"open_issues_count":6,"forks_count":12,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-07-02T21:49:47.727Z","etag":null,"topics":["blazor"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsakamoto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-14T13:21:48.000Z","updated_at":"2025-05-29T10:38:56.000Z","dependencies_parsed_at":"2024-11-15T12:16:35.099Z","dependency_job_id":null,"html_url":"https://github.com/jsakamoto/Toolbelt.Blazor.HttpClientInterceptor","commit_stats":{"total_commits":66,"total_committers":2,"mean_commits":33.0,"dds":"0.030303030303030276","last_synced_commit":"124f48b68c965b4cc91f213a5598fa568439d41b"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/jsakamoto/Toolbelt.Blazor.HttpClientInterceptor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsakamoto%2FToolbelt.Blazor.HttpClientInterceptor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsakamoto%2FToolbelt.Blazor.HttpClientInterceptor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsakamoto%2FToolbelt.Blazor.HttpClientInterceptor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsakamoto%2FToolbelt.Blazor.HttpClientInterceptor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsakamoto","download_url":"https://codeload.github.com/jsakamoto/Toolbelt.Blazor.HttpClientInterceptor/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsakamoto%2FToolbelt.Blazor.HttpClientInterceptor/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266737719,"owners_count":23976392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor"],"created_at":"2024-11-15T05:41:17.377Z","updated_at":"2025-07-23T19:04:59.710Z","avatar_url":"https://github.com/jsakamoto.png","language":"C#","readme":"# Blazor WebAssembly (client-side) HttpClient Interceptor [![NuGet Package](https://img.shields.io/nuget/v/Toolbelt.Blazor.HttpClientInterceptor.svg)](https://www.nuget.org/packages/Toolbelt.Blazor.HttpClientInterceptor/)\n\n## Summary\n\nThe class library that intercept all of the sending HTTP requests on a client side Blazor WebAssembly application.\n\n## Supported Blazor versions\n\n\"Blazor WebAssembly App (client-side) HttpClient Interceptor\" ver.9.x supports Blazor WebAssembly App version 3.2 Preview 2+, and Release Candidates, **of course, 3.2.x official release,** and **.NET 5, 6, 7 are also supported.**\n\n## How to install and use?\n\n**Step.1** Install the library via NuGet package, like this.\n\n```shell\n\u003e dotnet add package Toolbelt.Blazor.HttpClientInterceptor\n```\n\n**Step.2** Register `IHttpClientInterceptor` service into the DI container, at `Main()` method in the `Program` class of your Blazor application.\n\n```csharp\nusing Toolbelt.Blazor.Extensions.DependencyInjection; // \u003c- Add this, and...\n...\npublic class Program\n{\n  public static async Task Main(string[] args)\n  {\n    var builder = WebAssemblyHostBuilder.CreateDefault(args);\n    builder.RootComponents.Add\u003cApp\u003e(\"app\");\n    builder.Services.AddHttpClientInterceptor(); // \u003c- Add this!\n    ...\n```\n\n**Step.3** Add invoking `EnableIntercept(IServiceProvider)` extension method at the registration of `HttpClient` to DI container.\n\n```csharp\npublic class Program\n{\n  public static async Task Main(string[] args)\n  {\n    ...\n    builder.Services.AddScoped(sp =\u003e new HttpClient { \n      BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) \n    }.EnableIntercept(sp)); // \u003c- Add this!\n    ...\n```\n\nThat's all.\n\nYou can subscribe the events that will occur before/after sending all of the HTTP requests, at anywhere you can get `IHttpClientInterceptor` service from the DI container.\n\n```csharp\n@using Toolbelt.Blazor\n@inject IHttpClientInterceptor Interceptor\n...\n@code {\n  protected override void OnInitialized()\n  {\n    this.Interceptor.BeforeSend += Interceptor_BeforeSend;\n    ...\n  }\n\n  void Interceptor_BeforeSend(object sender, HttpClientInterceptorEventArgs e)\n  {\n    // Do something here what you want to do.\n  }\n  ...\n```\n\nIf you want to do async operations inside the event handler, please subscribe async version events such as `BeforeSendAsync` and `AfterSendAsync`, instead of `BeforeSend` and `AfterSend`.\n\n\u003e _Note:_ Please remember that if you use `IHttpClientInterceptor` to subscribe `BeforeSend`/`BeforeSendAsync`/`AfterSend`/`AfterSendAsync` events **in Blazor components (.razor),** you should unsubscribe events when the components is discarded. To do it, you should implement `IDisposable` interface in that component, like this code:\n\n```csharp\n@implements IDisposable\n...\npublic void Dispose()\n{\n  this.Interceptor.BeforeSend -= Interceptor_BeforeSend;\n}\n```\n\n### The arguments of event handler\n\nThe event handler for `BeforeSend`/`BeforeSendAsync`/`AfterSend`/`AfterSendAsync` events can be received `HttpClientInterceptorEventArgs` object.\n\nThe `HttpClientInterceptorEventArgs` object provides you to a request object and a response object that is come from an intercepted HttpClinet request.\n\n```csharp\nvoid OnAfterSend(object sender, HttpClientInterceptorEventArgs args)\n{\n  if (!args.Response?.IsSuccessStatusCode) {\n    // Do somthing here for handle all errors of HttpClient requests.\n  }\n}\n```\n\n### To read the content at \"AfterSendAsync\" event handler\n\nIf you want to read the content in the `Response` object at the event handler for `AfterSendAsync` event, **don't** reference the `Response.Content` property **directly** to do it.\n\nInstead, please **use the return value of the `GetCapturedContentAsync()` method.**\n\n\u003e _Note:_ Please remember that the `GetCapturedContentAsync()` method has a little bit performance penalty.  \n\u003e Because in the `GetCapturedContentAsync()` method, the `LoadIntoBufferAsync()` method of the `Response.Content` property is invoked.\n\n```csharp\nasync Task OnAfterSendAsync(object sender, HttpClientInterceptorEventArgs args)\n{\n  // 👇 Don't reference \"args.Response.Content\" directly to read the content.\n  // var content = await args.Response.Content.ReadAsStringAsync()\n\n  // 👇 Instead, please use the return value of the \"GetCapturedContentAsync()\" method.\n  var capturedContent = await arg.GetCapturedContentAsync();\n  var content = await capturedContent.ReadAsStringAsync();\n  ...\n```\n\n### Cancel a request before sending\n\nIf you want to cancel the request before sending it, you can do it by setting `false` to the `Cancel` property of the `BeforeSend`/`BeforeSendAsync` event argument.\n\n```csharp\nvoid OnBeforeSend(object sender, HttpClientInterceptorEventArgs args)\n{\n  if (/*something wron*/) {\n    // 👇 Setting the \"Cancel\" to true will cancel sending a request.\n    args.Cancel = true;\n  }\n}\n```\n\nIn that case, a HttpClient object will return the HttpResponseMessage object with HTTP status code **\"NoContent\" (HTTP 204)** to the caller, and also the `AfterSend`/`AfterSendAsync` event will not be fired.\n\n## Release Notes\n\nRelease notes is [here.](https://github.com/jsakamoto/Toolbelt.Blazor.HttpClientInterceptor/blob/master/RELEASE-NOTES.txt)\n\n## License\n\n[Mozilla Public License Version 2.0](https://github.com/jsakamoto/Toolbelt.Blazor.HttpClientInterceptor/blob/master/LICENSE)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsakamoto%2Ftoolbelt.blazor.httpclientinterceptor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsakamoto%2Ftoolbelt.blazor.httpclientinterceptor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsakamoto%2Ftoolbelt.blazor.httpclientinterceptor/lists"}