make an external api call in csharp

To make an external API call in C#, you can use the HttpClient class from the System.Net.Http namespace.

Here's an example of how to make a GET request to an API endpoint:

main.cs
using System.Net.Http;
using System.Threading.Tasks;

public static async Task<string> MakeApiRequestAsync(string endpointUrl)
{
    using (var httpClient = new HttpClient())
    {
        var response = await httpClient.GetAsync(endpointUrl);

        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadAsStringAsync();
        }

        return null;
    }
}
400 chars
18 lines

In this example, the HttpClient instance sends a GET request to the API endpoint specified in the endpointUrl parameter. async and await keywords are used to handle the asynchronous nature of the request and response. The response is checked for IsSuccessStatusCode and the response content is returned as a string. If the response is not successful, a null value is returned.

gistlibby LogSnag