There are many programming langages and process to make HTTP call are different in each language. So, Here you can find some language code snippets for GET API requests.

  • CURL
  • Go
  • JAVA - OkHttp
  • JavaScript - fetch
  • JavaScript - jQuery
  • NodeJS - request
  • NodeJS - axios
  • PHP - cURL
  • PHP - pecl_http
  • Python - http.client
  • Python - requests
  • Ruby - Net::HTTP

CURL

curl --request [your_api_endpoint]

Go

package main

import (
  "fmt"
  "net/http"
  "io/ioutil"
)

func main() {
        url := [your_api_endpoint]
        method := "GET"

        client := &http.Client { }
        req, err := http.NewRequest(method, url, nil)

        if err != nil {
        fmt.Println(err)
        }
        res, err := client.Do(req)
        defer res.Body.Close()
        body, err := ioutil.ReadAll(res.Body)

        fmt.Println(string(body))
}

JAVA - OkHttp

OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
  .url([your_api_endpoint])
  .method("GET", null)
  .build();
Response response = client.newCall(request).execute();

JavaScript - fetch

var requestOptions = {
  method: "GET",
  redirect: "follow",
}

fetch([your_api_endpoint], requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.log("error", error))

JavaScript - jQuery

var settings = {
  url: [your_api_endpoint],
  method: "GET",
  timeout: 0,
}

$.ajax(settings).done(function (response) {
  console.log(response)
})

NodeJS - request

var request = require('request');
var options = {
  'method': 'GET',
  'url': [your_api_endpoint]',
  'headers': {
  }
};
request(options, function (error, response) {
  if (error) throw new Error(error);
  console.log(response.body);
});

NodeJS - axios

axios
  .get([your_api_endpoint])
  .then(function (response) {
    console.log(response)
  })
  .catch(function (error) {
    console.log(error)
  })

PHP - cURL

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => [your_api_endpoint],
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

PHP - pecl_http

<?php
$client = new http\Client;
$request = new http\Client\Request;
$request->setRequestUrl([your_api_endpoint]);
$request->setRequestMethod('GET');
$request->setOptions(array());

$client->enqueue($request)->send();
$response = $client->getResponse();
echo $response->getBody();

Python - requests

import requests
url = [your_api_endpoint]
response = requests.request("GET", url, headers=headers, data = payload)
print(response.text.encode('utf8'))

Ruby - Net::HTTP

require "uri"
require "net/http"

url = URI([your_api_endpoint])

http = Net::HTTP.new(url.host, url.port);
request = Net::HTTP::Get.new(url)

response = http.request(request)
puts response.read_body

We get all these code snippets from Postman Labs. If you found any issues just let us know and we will update this Article.

Yes, That's all. If you have any other use cases then let us know about that. So, we can make an blog post about that.