How to download any result from an API using different languages
programming

How to download any result from an API using different languages

8/11/2023

What is HTTP

The Hypertext Transfer Protocol (HTTP) is the foundation of the World Wide Web, and is used to load webpages using hypertext links. HTTP is an application layer protocol designed to transfer information between networked devices and runs on top of other layers of the network protocol stack. A typical flow over HTTP involves a client machine making a request to a server, which then sends a response message.

what is http


What is JSON?

JSON stands for JavaScript Object Notation JSON is a lightweight format for storing and transporting data JSON is often used when data is sent from a server to a web page JSON is "self-describing" and easy to understand

what is jsonp


What is an API?

API stands for Application Programming Interface. A Web API is an application programming interface for the Web. A Browser API can extend the functionality of a web browser. A Server API can extend the functionality of a web server. A web API is an application programming interface for either a web server or a web browser. As a web development concept, it can be related to a web application's client side.

Why do developers need this code?

In the modern development world, developers will access various APIs in their career, everything can be accessed from APIs from IP Resolver functions to Machine Learning APIs.

Download a string VB6


Dim objHttp As Object, strURL as string, strText as string

Set objHttp = CreateObject("MSXML2.ServerXMLHTTP")

strURL = "http://www.yoursite.com/"

objHttp.Open "GET", strURL, False
objHttp.setRequestHeader "User-Agent", _
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHttp.Send ("")

strText = objHttp.responseText

Set objHttp = Nothing

VB . NET


    Dim result = CallApi(Of User)("https://api.url")

    Function CallApi(Of T)(Url As String) As T
        Dim handler As New HttpClientHandler()
        Dim Uri As New Uri(Url)
        Dim client As New HttpClient()
        handler.UseDefaultCredentials = True
        client = New HttpClient(handler)
        client.BaseAddress = New Uri(Url)
        client.DefaultRequestHeaders.Accept.Clear()
        client.DefaultRequestHeaders.Accept.Add(New MediaTypeWithQualityHeaderValue("application/json"))
        client.Timeout = TimeSpan.FromSeconds(60)

        Dim response = client.GetAsync(Url).GetAwaiter().GetResult()

        If response.IsSuccessStatusCode Then
            Dim apiResponse = response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
            Return JsonConvert.DeserializeObject(Of T)(apiResponse)
        Else
            Return Activator.CreateInstance(Of T)()
        End If

        Return Activator.CreateInstance(Of T)()
    End Function

C# .Net Core


IEnumerable = await CallApi(“https://api.url”)
private async Task CallApi(string url)
    {
        // Create a client handler that uses the proxy
        var httpClientHandler = new HttpClientHandler
        {            
            UseDefaultCredentials = true
        };

        httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;

        using (var client = new HttpClient(handler: httpClientHandler, disposeHandler: true))
        {
            using (var response = await cient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    string apiResponse = await response.Content.ReadAsStringAsync();
                    return JsonConvert.DeserializeObject(apiResponse);
                }                
            }
        }

        return Activator.CreateInstance();
    }

JQuery


$.getJSON(url, function (data) {
	Var json = data;	
});

This article briefly talks about what is an API, what is JSON and the code needed to call an api in vb, c#, cb6 and javascript so you can call an api.