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 5 years ago
- Comments: 15 (5 by maintainers)
There’s an open issue to allow not to set the
UserAgentby assigningnullto theRestClientOptions.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
If a HttpClient is passed to the constructor and it does not have a base address set it results in:
Also note as per my original message that the Base URL still has to be explicitly set even if the HttpClient base address is set or some posts cause a bad request issue. The following code has everything you need to test. I created a connection object as follows:
A constructor that takes a REST Client and a single method that tries to get an image from the API I am working with.
This is the Console application I was using to try and find the source of the problem:
As you can see the code here is also extremely simple it creates a new HttpClient, and creates a RestClient from it, and calling the connection method causes the above exception to be printed.
Setting the base URL for the HttpClient solves the exception problem but cause a BadRequest response from the API.
Setting both the base url for the HttpClient and explicitly setting the base URL for the Rest client works.