REST (REpresentational State Transfer) has emerged as the standard architectural design for web services and web APIs in these recent years.
This post will demonstrate how easy it is to use the Delphi REST Client Library to access Python REST Server (Flask microframework).
Prerequisites: If this is your first time to set up Delphi’s REST Client Library in RAD Studio for accessing any REST-based web services, you can read this article to set them up. But, there are several differences in configurations for accessing Flask’s Python REST server. This article will show you the details about it. Also, you can read this tutorial to set up your first Python Flask REST Server.
Table of Contents
1. Turn-On your Flask Server
Turn on your Flask server, by navigating to your project’s path, and run this command:
This is how it looks like in the command prompt:
2. Creating the Project
Open your RAD Studio and create a new project:
- For Delphi, choose File > New > Multi-Device Application – Delphi > Blank Application.
3. Adding the REST Components
- Drop the TRESTClient, TRESTRequest, and TRESTResponse components on the form.
2. In the Object Inspector, set the BaseURL property of the TRESTClient to http://127.0.0.1:5000 (your Flask’s server)
3. Select the TRESTRequest component on the form and set the following properties:
- In the Object Inspector, set the Resource property to companies.
- In the Structure View, expand the Params node.
- Right-click the Params node, and click Add item on the context menu.
- Under Params, click the newly added parameter 0-.
- In the Object Inspector, set the parameter name to id, and the parameter value to 1.
4. Connecting to Python Flask Server
Right-click the TRESTRequest component and select Execute. If you are connected successfully, this window will shows up:
Congratulations! You have integrated what “seems complicated concept” Python REST Server with Delphi, using Delphi’s REST Client.
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.