From 25a7f072154fb1691afb6a3e675dabeaf10312a5 Mon Sep 17 00:00:00 2001 From: Alexey Zimarev Date: Mon, 7 Feb 2022 20:20:14 +0100 Subject: [PATCH] Add the possibility to configure default headers of HttpClient --- src/RestSharp/RestClient.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/RestSharp/RestClient.cs b/src/RestSharp/RestClient.cs index 0727cc671..e55f63c3b 100644 --- a/src/RestSharp/RestClient.cs +++ b/src/RestSharp/RestClient.cs @@ -13,6 +13,7 @@ // limitations under the License. using System.Net; +using System.Net.Http.Headers; using System.Text; using RestSharp.Authenticators; using RestSharp.Extensions; @@ -38,7 +39,7 @@ public partial class RestClient : IDisposable { internal RestClientOptions Options { get; } - public RestClient(RestClientOptions options) { + public RestClient(RestClientOptions options, Action? configureDefaultHeaders = null) { UseDefaultSerializers(); Options = options; @@ -51,8 +52,8 @@ public RestClient(RestClientOptions options) { var finalHandler = Options.ConfigureMessageHandler?.Invoke(handler) ?? handler; HttpClient = new HttpClient(finalHandler); - ConfigureHttpClient(HttpClient); + configureDefaultHeaders?.Invoke(HttpClient.DefaultRequestHeaders); } /// @@ -85,6 +86,7 @@ public RestClient(HttpClient httpClient, RestClientOptions? options = null, bool Options = options ?? new RestClientOptions(); CookieContainer = new CookieContainer(); _disposeHttpClient = disposeHttpClient; + if (httpClient.BaseAddress != null && Options.BaseUrl == null) { Options.BaseUrl = httpClient.BaseAddress; }