Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Core 3.0 Consume API Error 'An invalid argument was supplied'

$
0
0

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"


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>