Home > Blockchain >  How to call third party REST API from ASP.NET Core Web API?
How to call third party REST API from ASP.NET Core Web API?

Time:08-20

I have been working with .NET Core for only a few weeks. Now I have a question about a Web API that I created for a project.

I need to call an external API, however all the documentation I see is for applications with MVC. This means they have a main page, which I don't have and don't want, because I just want to call this API on an new endpoint and to be consume on client side.

If I test the external API in postman it returns something like this:

postman response

I tried to follow along with the official documentation of Microsoft Call a Web API From a .NET Client (C#) however I get to a point where I'm not sure how to proceed.

I've create a model with a few of the result properties that I want to capture from the call to this endpoint shown in the first image and a simple controller that I want to be the final endpoint that return the information from this external API.

These are the things that I have already:

Model class Product:

namespace MyProyect
{
    public class Product
    {
        public string Id { get; set; } = string.Empty;
        public string Name { get; set; } = string.Empty;
        public string Type { get; set; } = string.Empty;
    }
}

Controller to consume the external API:

namespace MyProyect.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ThirdPartyAPIController : ControllerBase
    {
        static HttpClient client = new HttpClient();

        [HttpGet]
        static async Task<Product> GetProductsAsync(string path)
        {
            Product product = null;

            HttpResponseMessage response = await client.GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                product = await response.Content.ReadAsAsync<Product>();
            }

            return product!;
        }
    }
}

I don't really know how to proceed from here, this is all I can get from the documentation. I want this controller to be an endpoint that the user can consume. But this is not even appearing on the Swagger UI screen.

Is there an easy way to do this? Am I using an incorrect way?

CodePudding user response:

First register a HttpClient for your Controller/Service

// In Startup.cs (5.0)
services.AddHttpClient<ThirdPartyAPIController>();

// ... or in Program.cs (6.0)
builder.Services.AddHttpClient<ThirdPartyAPIController>();

Then inject the HttpClient and use it in the Controller method.

namespace MyProyect.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ThirdPartyAPIController : ControllerBase
    {
        // Let the `IHttpClientFactory` do the `HttpClient` handling
        private HttpClient _client;

        public ThirdPartyAPIController(HttpClient client)
        {
            _client = client;
        }

        [HttpGet]
        // use `public` instead of `static`
        // only this is then visible in Swagger UI
        public async Task<Product> GetProductsAsync(string path)
        {
            Product product = null;

            HttpResponseMessage response = await _client .GetAsync(path);

            if (response.IsSuccessStatusCode)
            {
                product = await response.Content.ReadAsAsync<Product>();
            }

            return product!;
        }
    }
}

Like written in the code comment. You have to use public (not static) for the controller method to be registered and visible in Swagger UI.

  • Related