I have an MVC application that I have written in .net Core, a very small part of the application contains a couple API endpoints that will be access via a console application at a set time. None of this is new and I have been using the same code for several years to do so, but for whatever reason in Core I am now getting errors. Listed below is the code:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://localhost:44383/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync("/api/test");
if (response.IsSuccessStatusCode)
{
var apiresults = await response.Content.ReadAsAsync<List<string>>();
}
}
And the error I receive is:
An invalid argument was supplied
//////////////////
Just to make sure I covered all bases I also wrote the code as such:
System.Collections.Generic.List<string> apiresults = new System.Collections.Generic.List<string>();
using (var httpClient = new System.Net.Http.HttpClient())
{
using (var response = await httpClient.GetAsync("https://localhost:44383/api/test"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
apiresults = Newtonsoft.Json.JsonConvert.DeserializeObject<System.Collections.Generic.List<string>>(apiResponse);
}
}
// Using RestClient
private static RestClient client = new RestClient("https://localhost:44383/api/");
RestRequest request = new RestRequest("Test", Method.GET);
IRestResponse<List<string>> response = client.Execute<List<string>>(request);
All three should work, but instead all three give the exact same error "An invalid argument was supplied"