1

I'm working with a solution that was built in ASP.NET Core 2.1 as a console application, however it works like a web API. I need to call an external API, which was created in ASP.NET as well, to receive data in JSON format by creating an API in this system to get the values and reorganize them into different tables.

Basically, I need to call an API with another API. Is there any way I could achieve this?, and how should I do it? I've tried lots of code that I found online but none seems to work because of the ASP.NET Core version.

Thanks in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sthefany Rangel
  • 13
  • 1
  • 1
  • 4
  • None work because of the ASP.NET core version? You are going to have to provide a lot more detail and possibly some code in order to get some assistance. Your solution A, needs to call an external API B. You should simply be able to make a standard request to API B. I'm not clear on if you are simply calling the external API (http request) or attempting to integrate the two solutions so you can access B directly. – Anu6is Sep 09 '21 at 21:16
  • I'm just trying to call an external API via HttpRequest or something like so. I have tried using WebRequest and it has'nt worked out properly. What kind of code would you like me to post?, so that I can look for it and update the question. – Sthefany Rangel Sep 09 '21 at 22:14
  • 1
    Post the code you tried that didn't work. It should at least give other an idea of if you simply made a mistake or if you should try a completely separate approach. – Anu6is Sep 09 '21 at 22:19
  • Does this answer your question? [Can I get data from web api in .net core with another api or api key?](https://stackoverflow.com/questions/69029517/can-i-get-data-from-web-api-in-net-core-with-another-api-or-api-key) – LazZiya Sep 10 '21 at 12:21

2 Answers2

2

As others mentioned use/inject a http Client

  • In your Startup.cs in the ConfigureServices do something like:
services.AddHttpClient("THE_Client", c =>
{
    c.BaseAddress = new Uri(Configuration.GetValue<string>("ClientURL"));
});
  • In your Controller (where you want to call the other API) add Constructor injection:
public TheController(HttpClientFactory clientFactory)
{
    _clientFactory = clientFactory ?? throw new ArgumentNullException(nameof(clientFactory));
}
  • use the http client
// Create client
HttpClient client = _clientFactory.CreateClient("THE_Client");

// send request
var builder = new UriBuilder(client.BaseAddress)
{
    Path = "api/v1/ENDPOINT"
};

var request = new HttpRequestMessage
{
    RequestUri = builder.Uri,
    Method = HttpMethod.Post,
    Content = "blah"
};
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Kraego
  • 2,978
  • 2
  • 22
  • 34
0

The easiest way to do it is by using the built-in HTTP Client

But in case you are looking for performance and you are able to upgrade your dotnet version, you might be interested in Http2 gRPC protocol.

Leonardo
  • 34
  • 5
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 10 '21 at 06:06