restsharp / restsharp/RestSharp
RestClientOptions.UserAgent missing from requests with useClientFactory: true
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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