locked
Core 3.0 Consume API Error 'An invalid argument was supplied' RRS feed

  • Question

  • User258730882 posted

    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"

    Thursday, February 27, 2020 2:36 PM

Answers

  • User475983607 posted

    I can't reproduce the errors.  My test code is below.  FYI, I'm using System.Text.Json; not NewtonSoft.

    Client

    using ConsoleApp1.Models;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using System.Text.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
    
        class Program
        {
            private static HttpClient client;
            static async Task Main(string[] args)
            {
                client = new HttpClient();
    
                List<string> apiresults = await Get();
                foreach (string s in apiresults)
                {
                    Console.WriteLine(s);
                }
            }
    
            private static async Task<List<string>> Get()
            {
    
                client.BaseAddress = new Uri("https://localhost:5001/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                HttpResponseMessage response = await client.GetAsync("/api/test");
                if (response.IsSuccessStatusCode)
                {
                    return await JsonSerializer.DeserializeAsync<List<string>>(await response.Content.ReadAsStreamAsync());
                }
                else
                {
                    throw new Exception($"Error: {response.StatusCode}");
                }
            }
        }
    }
    

    API

    using Microsoft.AspNetCore.Mvc;
    
    namespace WebApi.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class TestController : ControllerBase
        {
            [HttpGet]
            public string[] Get()
            {
                return new string[] { "Hello", "World" };
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2020 7:36 PM

All replies

  • User-474980206 posted

    You should use postman and see the actual json created.

    Additionally, you add the swagger UI code to the api to allow testing with browser.

    Thursday, February 27, 2020 3:54 PM
  • User258730882 posted

    Its not the API, that works fine, it is the code attempting to make the connection.

    Thursday, February 27, 2020 3:57 PM
  • User258730882 posted

    This has to be a problem with .net Core within the console application. I recreated the console app as a .net Framework application, same code, works without issue. I wonder if there is a known issue in regards to api consumption with .net Core console applications and a possible work around?

    Thursday, February 27, 2020 6:34 PM
  • User475983607 posted

    I can't reproduce the errors.  My test code is below.  FYI, I'm using System.Text.Json; not NewtonSoft.

    Client

    using ConsoleApp1.Models;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using System.Text.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
    
        class Program
        {
            private static HttpClient client;
            static async Task Main(string[] args)
            {
                client = new HttpClient();
    
                List<string> apiresults = await Get();
                foreach (string s in apiresults)
                {
                    Console.WriteLine(s);
                }
            }
    
            private static async Task<List<string>> Get()
            {
    
                client.BaseAddress = new Uri("https://localhost:5001/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                HttpResponseMessage response = await client.GetAsync("/api/test");
                if (response.IsSuccessStatusCode)
                {
                    return await JsonSerializer.DeserializeAsync<List<string>>(await response.Content.ReadAsStreamAsync());
                }
                else
                {
                    throw new Exception($"Error: {response.StatusCode}");
                }
            }
        }
    }
    

    API

    using Microsoft.AspNetCore.Mvc;
    
    namespace WebApi.Controllers
    {
        [Route("api/[controller]")]
        [ApiController]
        public class TestController : ControllerBase
        {
            [HttpGet]
            public string[] Get()
            {
                return new string[] { "Hello", "World" };
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2020 7:36 PM
  • User258730882 posted

    Don't know that I would call your answer 100% the answer, but I will give credit as it did direct me to the right area:

    https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.1

    NewtonSoft was the problem

    Thursday, February 27, 2020 7:52 PM