Can't see the JSON result of a Get request in postman and shows empty string array

610 views Asked by At

I have an ASP.NET CORE application that sends a POST/GET request to a REST (Orthanc Rest API). The issue is I receive the result and convert it to a JSON, but postman shows as an empty array. here is my code:

// GET Method
public class PACSController : ControllerBase
    {

        // GET: api/PACS
        [HttpGet]
        public async Task<object> Get()
        {
            var result = await Orthanc.Orthanc.InstanceAsync();           
            return result;
        }
    }



public class Orthanc
    {
        public static string baseUrl = "https://demo.orthanc-server.com/";
        public static async Task<object> InstanceAsync()
        {

            string url = baseUrl + "instances";
            using (HttpClient client = new HttpClient())

            using (HttpResponseMessage res = await client.GetAsync(url))

            using (HttpContent content = res.Content)
            {
                string data = await content.ReadAsStringAsync();
                if (data != null)
                {
                    Console.WriteLine(data);
                }

                var jData = JsonConvert.DeserializeObject(new string[] { data }[0]);

                return jData;
            }

        }
    }

The result of request inside the code

Postman result

1

There are 1 answers

0
Rena On

As part of the work to improve the ASP.NET Core shared framework, Newtonsoft.Json has been removed from the ASP.NET Core shared framework for asp.net core 3.x.

Follow the steps:

Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson package on nuget.

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

Update Startup.ConfigureServices to call AddNewtonsoftJson.

services.AddControllersWithViews().AddNewtonsoftJson();

Reference:

https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#use-newtonsoftjson-in-an-aspnet-core-30-mvc-project