Bayesean Blog - Desktop, Mobile and IOT Developer Blog


Delphi REST VCL Client Basic Auth Part 2B

Posted on 17th Sep 2017 in Delphi > XE4


restserver.png

This covers Part 2B of the series...

Later in part 5 this will be covered in depth covering both the PHP Rest Server and Both VCL and Firemonkey Client in the areas of Basic Authentication, API Key Authentication and JWT Authentication.

Below is an extract on how to create basic Authentication and API Key Authentication for VCL Delphi using the built in Indy component library.

Handling a Basic Authorization Call using Username and Password:-

{Code}

var

http:TIDHttp;

begin

http := TIdHttp.Create(nil);

http.HandleRedirects := true;

http.ReadTimeout := 5000;

http.MaxAuthRetries := 0;

http.HTTPOptions := [hoInProcessAuth];

 

Http.Request.BasicAuthentication := True;

Http.Request.Username := name';

Http.Request.Password := 'yourpassword';

Http.Request.Accept := 'http';

Http.Request.ContentType := 'application/json';

Memo3.Lines.Text := http.Get('http://yourrestcservercall');

http.free;

 

This code shows how you would implement an APIKey using Get……. Note that the use of Custom Headers are used using ‘Authorization’ to send the APIKey. This method is preferable as it is more secure than including the APIKey into the body and sending the body only.

 

Code}
var
 http:TIDHttp;
begin
http := TIdHttp.Create(nil);
http.HandleRedirects := true;
http.ReadTimeout := 5000;
http.MaxAuthRetries := 0;
http.HTTPOptions := [hoInProcessAuth];

 Http.Request.CustomHeaders.Clear;
// Add the custom Header to use -Authorization and password -1234
 Http.Request.CustomHeaders.AddValue('Authorization','1234');
 Http.Request.BasicAuthentication := False;
 Http.Request.Accept := 'http';
 Http.Request.ContentType := 'application/json';
Memo1.Lines.Text := http.Get('http://YourRestServercall’);
http.free;

 

Happy Coding…


Be the First to Comment...

Add a Comment

9+5

Recent News

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB
Delphi Libusb Library Introduction
Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App
Delphi FMX Leaflet Plotter using OSM Maps
C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout

Categories

Bootstrap 4
Delphi VCL
Delphi FMX
Ajax
Bootstrap 3
CSS
XE4>Delphi > XE4
Delphi < XE4

Archives

October 2018

Delphi VCL Buttons in DBGrid
Two Helper Apps for Delphi LibUSB

September 2018

Delphi Libusb Library Introduction

August 2018

Delphi Object directly to a Json string in a REST Client
Delphi using Environment Variables in your App

July 2018

Delphi FMX Leaflet Plotter using OSM Maps

June 2018

C2PAS32 Convertor Application
C to Delphi Open Source Convertors Shootout
Delphi command-line programs with DOSCommand

May 2018

Delphi PDF Embedded viewer with PDF.js

March 2018

Delphi FMX - Changing TCharacter to TCharHelper
Make Your Delphi App POP using Javascript!

January 2018

Delphi FMX Dashboard using Chart.JS
Delphi FMX Form Docking

December 2017

PHP Slim REST Server & Delphi Auth Part 5

November 2017

Delphi FMX REST Client App Part 4

October 2017

Delphi VCL REST Pricing Client App Part 3

September 2017

Delphi REST VCL Client Basic Auth Part 2B

August 2017

Delphi REST Client Part 2A
PHP PDO REST Server Part 1

July 2017

PHP REST Server and Delphi Client Intro

June 2017

Delphi SQLite Encryptor-Decryptor Tool
Updating Applications Manifest using Delphi

May 2017

Create a Visual IP Address Geolocation with PHP

March 2017

PHP Downloader using Countdown timer
PHP File Downloader from a Inbox Selection

February 2017

Javascript Image-File Uploader with ThumbViewer

January 2017

Morris Charts and PHP-PDO

December 2016

CSS to create a functional Toggle Button