DEV Community

Sardar Mudassar Ali Khan
Sardar Mudassar Ali Khan

Posted on • Edited on

7

Get Data from Third Party API In Asp.net Core Web API

To retrieve data from a third-party API in an ASP.NET Core Web API application, you can use the HttpClient class, which provides methods to send HTTP requests and receive responses. Here's an example of how you can do it:

  1. Add the System.Net.Http package to your project. You can do this by right-clicking on your project in Visual Studio and selecting "Manage NuGet Packages." Then search for and install the System.Net.Http package.

  2. In your ASP.NET Core Web API controller, create an instance of the HttpClient class to make the API request. Here's an example of a GET request:

using System.Net.Http;
using Microsoft.AspNetCore.Mvc;

namespace YourNamespace.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DataController : ControllerBase
    {
        private readonly HttpClient _httpClient;

        public DataController()
        {
            _httpClient = new HttpClient();
        }

        [HttpGet]
        public async Task<IActionResult> GetData()
        {
            // Replace "API_URL" with the actual URL of the third-party API you want to access
            var apiUrl = "API_URL";

            HttpResponseMessage response = await _httpClient.GetAsync(apiUrl);

            if (response.IsSuccessStatusCode)
            {
                string data = await response.Content.ReadAsStringAsync();
                // Process the data or return it as-is
                return Ok(data);
            }
            else
            {
                // Handle the error condition
                return StatusCode((int)response.StatusCode);
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode
  1. Replace "API_URL" with the actual URL of the third-party API you want to access.

  2. In this example, the GetData method sends a GET request to the specified API URL. If the request is successful (HTTP status code 200), it reads the response content as a string and returns it as the API's response. Otherwise, it returns an appropriate error status code.

Note that you may need to configure additional settings for the HttpClient, such as base address, headers, or authentication, depending on the requirements of the third-party API you're accessing.

Neon image

Set up a Neon project in seconds and connect from a Next.js application

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay