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

Unexpected behavoir when upgrading to 111.3.0 #2235

Closed
hagaygo opened this issue Jun 30, 2024 · 5 comments · Fixed by #2241
Closed

Unexpected behavoir when upgrading to 111.3.0 #2235

hagaygo opened this issue Jun 30, 2024 · 5 comments · Fixed by #2241
Labels

Comments

@hagaygo
Copy link

hagaygo commented Jun 30, 2024

Hi

Upgraded a little internal tool which uses restsharp to use version 111.3.0 instead 110.2.0 and started to get the following error :

A default parameters with the same name has already been added

After quick check i found that the following code is the problem

var cli = new RestClient();
cli.AddDefaultHeader("User-Agent", "myagent");
return cli;

AddDefaultHeader throws the exception and changing the code to :

var cli = new RestClient();
cli.DefaultParameters.RemoveParameter("User-Agent", ParameterType.HttpHeader);
cli.AddDefaultHeader("User-Agent", "myAgent");
return cli;

Workarounds this problem.

Is this behavior is by design ?

@hagaygo hagaygo added the bug label Jun 30, 2024
@alexeyzimarev
Copy link
Member

I think it's a bug. By default, parameters with the same aren't allowed. You can always set AllowMultipleDefaultParametersWithSameName to true to override this. But, headers should be excluded as it's normal to have multiple values for the same header.

@alexeyzimarev
Copy link
Member

The real workaround is client.DefaultParameters.ReplaceParameter(parameter)

@alexeyzimarev
Copy link
Member

Also, setting the user agent is normally done by setting the options property:

var options = new RestClientOptions("https://xx.yy") { UserAgent = "my-agent" };
var client = new RestClient(options);

@hagaygo
Copy link
Author

hagaygo commented Jul 11, 2024

Not sure from you replies whether its gonna get changed.

Workarounds are fine , but as a of this moment , a code that worked fine on previous versions stopped working on newer versions.

Just wanted to report on this issue and it got acknowledged.

Thanks for your work and let me know if you need anything else on my part.

@hagaygo hagaygo closed this as completed Jul 11, 2024
@alexeyzimarev
Copy link
Member

I just mentioned the workaround while I included the fix to the PR I was opening for the other issue related to headers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants