Skip to content

RestClient GetJsonAsync parameters in uri and AddDefaultHeader with Authorization #1957

@federicocodo

Description

@federicocodo

Describe the bug
I was trying to perform some simply GET request using all the methods made available by the RestClient class (GetAsync(), GetAsync<T>() and GetJsonAsync<T>()). The first two requests worked great, but using the third one I found some problems.
In the constructor I added, for the RestClient, the base URL as written in the docs, and an Authorization ApiKey as a DefaultHeader; when performing the request I also added some parameters like the Id and a parameter to add in the query like this example taken from the documentation

var client = new RestClient("https://example.org");
var args = new {
    id = "123",
    foo = "bar"
};
// Will make a call to https://example.org/endpoint/123?foo=bar
var response = await client.GetJsonAsync<TResponse>("endpoint/{id}", args, cancellationToken);

Unfortunately this request give me a BadRequest when all the other methods work perfectly. It seems that the DefaultHeader Authorization, previously added, it isn't considered when performing the GET request but I don't have a specific error log.

To Reproduce
I don't have any test to show you because the test I use to find the error was with sensitive data.
But the code I use is like this:

public TestClient(ILogger<TestClient> logger, IConfiguration configuration)
{
    _logger = logger;
    _configuration = configuration;
    var options = new RestClientOptions(URL);
    _client = new RestClient(options).AddDefaultHeader(KnownHeaders.Authorization, APIKEY);
}

public async Task<TResponse> Get(string id, CancellationToken cancellationToken)
{
    var parameters = new
    {
      id = id,
      details = true
    };      
    
    var response = await _client.GetJsonAsync<TResponse>("info/{id}", parameters, cancellationToken);
}

Expected behavior
I expect that like the other request if I add my authorization api key in the RestClient with the AddDefaultHeader I can make request without problems and receive the response from the URL.
This url is composed as follows: http://BASE_URL/info/{id}?details=true

Stack trace

   at RestSharp.RestClientExtensions.<GetAsync>d__14`1.MoveNext()
   at WebRestClient.Services.TestClient.<GetCities>d__6.MoveNext() in C:\Users\fcodognotto\Projects\DaprCurse\RestClient\WebRestClient\Services\TestClient.cs:line 53
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\fcodognotto\Projects\DaprCurse\RestClient\TestClient\Program.cs:line 12

Desktop (please complete the following information):

  • OS: [Windows 11]
  • .NET version [.NET 6]
  • Version [e.g. 108.0.1]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions