restsharp / restsharp/RestSharp

RestClientOptions.UserAgent missing from requests with useClientFactory: true

Open Beginner friendly
#2,389 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
9.8k
Forks
2.3k
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

When useClientFactory: true is passed to the RestClient constructor and UserAgent is set via RestClientOptions.UserAgent, the User-Agent header is missing from outgoing requests for every RestClient instance after the first one sharing the same BaseUrl.

When disabling the client factory the User-Agent is back on every request.

To Reproduce


var options = new RestClientOptions
 {
     BaseUrl = new Uri("https://httpbin.org"),
     UserAgent = "MyApp/1.0"
 };

//1st request
var client1 = new RestClient(options, useClientFactory: true);
_ = await client1.GetAsync(new RestRequest("/get"));

// 2nd request
var client2 = new RestClient(options, useClientFactory: true);
_ = await client2.GetAsync(new RestRequest("/get"));

1st raw request from fiddler

GET https://httpbin.org/get HTTP/1.1
Host: httpbin.org
User-Agent: MyApp/1.0
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Accept-Encoding: gzip, deflate, br

2nd raw request from fiddler

GET https://httpbin.org/get HTTP/1.1
Host: httpbin.org
Accept: application/json, text/json, text/x-json, text/javascript, application/xml, text/xml
Accept-Encoding: gzip, deflate, br

Expected behavior

User-Agent should appear on every request regardless of how many RestClient instances are created for the same BaseUrl.

Desktop:

  • OS: Windows 10.0.26200.8457
  • .NET version: .NET 10.0.8
  • Version 114.0.0

Additional context

I suspect that ConfigureDefaultParameters should be called regardless of the client instance coming from cache or not.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the RestClient constructor path for useClientFactory: true and inspect ConfigureDefaultParameters when a cached client is reused. Reproduce the two-client example against the same BaseUrl, then verify that UserAgent is present on every outgoing request, including requests from the second instance.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.