RestSharp: Post Fails Using HttpClient in RestSharp Client constructor

While working with an API recently I encountered unexpected behavior when using an existing HttpClient in the RestClient constructor.

If I use the HttpClient in the constructor all my posts return with a bad request response (code 400), however creating the RestClient with the same base url as used in the HttpClient works.

To Reproduce

TestClass:

public class Connection {
    RestClient client;

    public Connection(bool wantFail)
    {
        string     baseUrl = @"https://travellermap.com/api/";
        HttpClient http    = new HttpClient();
        http.BaseAddress = new Uri(baseUrl);

        if (wantFail) {
            client = new RestClient(http);
        }
        else {
            client = new RestClient(baseUrl);
        }
    }

    public byte[] CreateSectorMapTest() {
        byte[] result;

       
        var request = new RestRequest("poster?subsector=A", Method.Post);
        request.AddHeader("Content-Type", "text/plain");
        request.AddHeader("Accept", "image/svg+xml");

        var body = @"#--------1---------2---------3---------4---------5-------
#PlanetName   Loc. UPP Code   B   Notes         Z  PBG Al
#----------   ---- ---------  - --------------- -  --- --
Cogrikonur    0101 C8C5446-9    Fl Ni              120 Im";

        request.AddParameter("text/plain", body, ParameterType.RequestBody);
        RestResponse response = client.Execute(request);

        if (response.IsSuccessful) {
            result = response.RawBytes;
        }
        else {
            result = null;
        }

        return result;
    }
}

Test Application Console:

Console.WriteLine("Hello, World!");

Console.WriteLine("Testing For Success");

Connection goodConnection = new Connection(false);
byte[] image = goodConnection.CreateSectorMapTest();

if (image != null) {
    Console.WriteLine("Good Connection Worked");
}
else {
    Console.WriteLine("Good Connection Failed");
}

Console.WriteLine("Testing For Fail");
Connection badConnection = new Connection(true);
byte[] imageBad = badConnection.CreateSectorMapTest();

if (imageBad != null) {
    Console.WriteLine("Bad Connection Worked");
}
else {
    Console.WriteLine("Bad Connection Failed");
}

Console.ReadLine();

Console.WriteLine("Finished");

Output of Console App: Hello, World! Testing For Success Good Connection Worked Testing For Fail Bad Connection Fail

Expected behavior

I would expect that the two constructors should provide the same functionality.

Desktop (please complete the following information):

  • OS: [e.g. Linux]
  • .NET version [e.g. .NET 6]
  • Version [e.g. 107.0.3.1-alpha.0.24] + in Clone of current repo

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

There’s an open issue to allow not to set the UserAgent by assigning null to the RestClientOptions.UserAgent. It’s impossible atm, but it will be fixed.

I had the same problem as described in the original question. In my case it turned out to be because the header “User-Agent” is not set by default in HttpClient whereas it turns out it is set by default by RestSharp. Setting this header on the HttpClient before passing it to the RestSharp constructor solved the problem for me, and it also made HttpClient work on its own.

The reason I started using RestSharp was because HttpClient failed. I then used RestSharp with a base address, but couldn’t sleep before I had figured out the reason why HttpClient failed. In my specific case I was trying to download an ics calendar file from https://outlook.office365.com/ The code I use is here: https://github.com/erlendthune/RememberCalendar