Home > Uncategorized > Using #Delphi to call a JSON based #API with basic authentication.

Using #Delphi to call a JSON based #API with basic authentication.

Delphi is quite an old programming language, and has been around since 1995. It’s not really one of the “cool kids” when it comes to programming. However, there is nothing stopping it interoperating with modern REST APIs, which this blog post will show.

So, first off, I downloaded the community edition of Delphi from Embarcadero, and started, directly after a “Hello World”, to make a request to a service that returns my external IP address. Just a simple HTTP GET, but that’s the first step.

So far so simple. I make a crazy call out to CoInitializeEx which does some COM initialization (COM – oh dear). Anyway, then I create a TidHTTP object, which comes from the Indy Project (https://www.indyproject.org/) and then makes a simple GET request, and prints the output to console.

In the above example, I am using an API from http://www.placaapi.com/ – which is a Brazilian vehicle lookup service. you need a username and password, which can be obtained free of charge from the website.

I create an Authentication object, and set the username and password, then I make a HTTP GET request as before.

After I receive the JSON, I pass this to TJSonObject, and read the Description Node from the JSON – There are plenty more fields here, but this is the easiest to display.

For those who want to copy/paste the code – it’s below;

program helloWorld;


{$R *.res}

  SysUtils, IdHTTP, ActiveX, IdAuthentication, System.JSON;

  HTTP: TIdHTTP;  // Create the Indy HTTP Library https://www.indyproject.org/
  Buffer: String;
  Description: string;
  Registration: string;

    // Enter the car registration here
    Registration :=  'BYP6404';
    // https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-coinitializeex
    CoInitializeEx(nil, COINIT_MULTITHREADED);
    HTTP := TIdHTTP.Create;
    HTTP.Request.BasicAuthentication:= true;
    HTTP.Request.Authentication := TIdBasicAuthentication.Create;
    HTTP.Request.Authentication.Username := '***USERNAME HERE***';
    HTTP.Request.Authentication.Password := '***PASSWORD HERE***';
    // Below is the API endpoint for Brazil, can be replaced with any country.
    Buffer := HTTP.Get('http://www.placaapi.com/api/json.aspx/CheckBrazil/' + Registration);
    //Writeln(Buffer); // Uncomment to see full result
    JsonValue := TJSonObject.ParseJSONValue(Buffer);
    // Read the Vehicle Decription
    Description := JsonValue.GetValue<string>('Description');
    on E: Exception do
        Writeln(E.ClassName, ': ', E.Message); // Show any error
  readln; // pause on completion

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: