Skip to content

Unexpected behavoir when upgrading to 111.3.0 #2235

Closed

Description

@hagaygo

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 ?

Activity

  1. alexeyzimarev commented on Jul 11, 2024

    @alexeyzimarev
    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.

  2. alexeyzimarev commented on Jul 11, 2024

    @alexeyzimarev
    Member

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

  3. alexeyzimarev commented on Jul 11, 2024

    @alexeyzimarev
    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);
  4. hagaygo commented on Jul 11, 2024

    @hagaygo
    Author

    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.

  5. alexeyzimarev commented on Jul 11, 2024

    @alexeyzimarev
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions