Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the possibility to configure default headers of HttpClient #1738

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/RestSharp/RestClient.cs
Expand Up @@ -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;
Expand All @@ -38,7 +39,7 @@ public partial class RestClient : IDisposable {

internal RestClientOptions Options { get; }

public RestClient(RestClientOptions options) {
public RestClient(RestClientOptions options, Action<HttpRequestHeaders>? configureDefaultHeaders = null) {
UseDefaultSerializers();

Options = options;
Expand All @@ -51,8 +52,8 @@ public partial class RestClient : IDisposable {
var finalHandler = Options.ConfigureMessageHandler?.Invoke(handler) ?? handler;

HttpClient = new HttpClient(finalHandler);

ConfigureHttpClient(HttpClient);
configureDefaultHeaders?.Invoke(HttpClient.DefaultRequestHeaders);
}

/// <summary>
Expand Down Expand Up @@ -85,6 +86,7 @@ public partial class RestClient : IDisposable {
Options = options ?? new RestClientOptions();
CookieContainer = new CookieContainer();
_disposeHttpClient = disposeHttpClient;

if (httpClient.BaseAddress != null && Options.BaseUrl == null) {
Options.BaseUrl = httpClient.BaseAddress;
}
Expand Down