Posted on Leave a comment

How to make a basic REST call in 8 different languages

To get you started, here are some examples for making a basic REST HTTP call, in 12 languages:

Java

OkHttp
   OkHttpClient client = new OkHttpClient();
   Request request = new Request.Builder()
     .url(“https://api-trial.addressian.co.uk/address/[your_address]”)
     .get()
     .addHeader(“x-api-key”, “[your_api_key]”)
   .build();
   Response response = client.newCall(request).execute();

Unirest
   HttpResponse<String> response = Unirest.get(“https://api-trial.addressian.co.uk/address/[your_address]”)
     .header(“x-api-key”, “[your_api_key]”)
     .asString();

cURL

   curl –request GET \
     –url ‘https://api-trial.addressian.co.uk/address/[your_address]’ \
     –header ‘x-api-key: [your_api_key]’

Wget

   wget –quiet \
     –method GET \
     –header ‘x-api-key: [your_api_key]’ \
     –output-document \
     – ‘https://api-trial.addressian.co.uk/address/[your_address]’

Javascript

   XMLHttpRequest
   var data = null;

   var xhr = new XMLHttpRequest();
   xhr.withCredentials = true;

   xhr.addEventListener(“readystatechange”, function () {
     if (this.readyState === this.DONE) {
       console.log(this.responseText);
     }
   });

   xhr.open(“GET”, “https://api-trial.addressian.co.uk/address/[your_address]”);
   xhr.setRequestHeader(“x-api-key”, “[your_api_key]”);

   xhr.send(data);

jQuery
   var settings = {
     “async”: true,
     “crossDomain”: true,
     “url”: “https://api-trial.addressian.co.uk/address/[your_address]”,
     “method”: “GET”,
     “headers”: {
     “x-api-key”: “[your_api_key]”
     }
   }

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

PHP

ext-curl
   <?php

      $curl = curl_init();

        curl_setopt_array($curl, array(
        CURLOPT_URL => “https://api-trial.addressian.co.uk/address/[your_address]”,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => “”,
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => “GET”,
        CURLOPT_HTTPHEADER => array(
        “x-api-key: [your_api_key]”
        ),
      ));

      $response = curl_exec($curl);
     $err = curl_error($curl);

     curl_close($curl);

      if ($err) {
         echo “cURL Error #:” . $err;
      } else {
         echo $response;
   }

pecl/http v2

   <?php

     $client = new http\Client;
     $request = new http\Client\Request;

     $request->setRequestUrl(‘https://api-trial.addressian.co.uk/address/[your_address]’);
     $request->setRequestMethod(‘GET’);
     $request->setHeaders(array(
        ‘x-api-key’ => ‘[your_api_key]
     ));

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

     echo $response->getBody();

C#

   var client = new RestClient(“https://api-trial.addressian.co.uk/address/[your_address]”);
   var request = new RestRequest(Method.GET);
   request.AddHeader(“x-api-key”,[your_api_key]“);
   IRestResponse response = client.Execute(request);

Python 3

   import http.client

   conn = http.client.HTTPSConnection(“api-trial.addressian.co.uk”)
   headers = { ‘x-api-key’: “[your_api_key]” }
   conn.request(“GET”, “/address/[your_address]”, headers=headers)
   res = conn.getresponse()
   data = res.read()

   print(data.decode(“utf-8”))

Node

   var http = require(“https”);

   var options = {
      “method”: “GET”,
      “hostname”: “api-trial.addressian.co.uk”,
      “port”: null,
      “path”: “/address/[your_address]”,
      “headers”: {
         “x-api-key”: “[your_api_key]”
      }
   };

   var req = http.request(options, function (res) {
   var chunks = [];

   res.on(“data”, function (chunk) {
      chunks.push(chunk);
   });

   res.on(“end”, function () {
      var body = Buffer.concat(chunks);
      console.log(body.toString());
      });
   });

   req.end();

Leave a Reply