Using Delegating Handlers

When directly creating a RestClient instance then simply pass one or more DelegatingHandlers to the Config constructor.

This example uses two DelegatingHandlers to add headers to the request:

public class MyHandler1 : DelegatingHandler
{
   protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
   {
      request.Headers.Add("TestHandlerHeader1", "TestHandler1");     
      return await base.SendAsync(request, token); //Call next handler in pipeline
   }
}

public class MyHandler2 : DelegatingHandler
{
   protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken token)
   {
      request.Headers.Add("TestHandlerHeader2", "TestHandler2");     
      return await base.SendAsync(request, token); //Call next handler in pipeline
   }
}

var restClient = new RestClient("http://jsonplaceholder.typicode.com", 
                                new Config(new MyHandler1(), new MyHandler2()));

When using IHttpClientFactory use the UseHandler extension method when adding the RestClient to your .NET Core DI container (usually in Startup.cs):

services
      .AddRestClient("http://jsonplaceholder.typicode.com")
      .UseHandler(() => new MyHandler1())
      .UseHandler(() => new MyHandler2());

Note you can use one HttpClientHandler, and any number of DelegatingHandlers or HttpMessageHandlers.