DelphiPythonPython GUIWindows

Learn To Work With HTTP Requests Using Python Requests Library In A Delphi Windows App

blogbanner3-30

We have learned many possibilities and examples in combining Python4Delphi with Python language features and various libraries. From image processing, numerical and scientific computing, computer vision, you name it. Now, how about interaction with the internet, such as HTTP Requests?

Python4Delphi or P4D with Requests library will allow you to execute HTTP requests in Python GUI for Windows.

Requests is a simple, yet elegant HTTP library. Requests allow you to execute standard HTTP requests. Using this library, you can pass parameters to requests, add headers, receive and process responses, execute authenticated requests.

Requests is one of the most downloaded Python packages today, pulling in around 14M downloads/week—according to GitHub, Requests is currently depended upon by 500,000+ repositories. Knowing these facts, you may certainly put your trust in this credible library.

This post will guide you on how to work with the HTTP Requests using the Requests library and use Python for Delphi to display it in the Delphi Windows GUI app.

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

0_rundemo1-6813476

 

1. Make GET Request

Let’s create a very basic example of GET requests. Just use the get() method and pass the URL to this method. 

From the response object, you can get a lot of useful information. This example shows how to get content, status, and list of response headers:

The result in Python4Delphi GUI:

1_1-3453176

 

2. POST Request with Payload and Timeout

With the Requests library, you can perform post requests by calling the post() method. It is also possible to pass input data to the parameter payload

Different types of input data are also possible. For example, dictionaries, tuples, lists:

The results:

1_4-8817220

 

3. Authenticated Request

In this example we will take a look at how to execute authenticated requests. It is very easy, just pass the username and the password in the auth parameter. If authorization is successful, then we will receive a response status code 200, otherwise there should be non-authorization error 404.

The response in Python4Delphi:

1_5-1664168

Let’s try more interesting example using GitHub API:

Let’s see the results:

1_3-8076189

If this is your first time using GitHub API and you don’t know how to generate your access token, this is the link to guide you.

Requests allow us to send HTTP/1.1 requests extremely easily. There’s no need to manually add query strings to your URLs, or to form-encode your PUT & POST data — but nowadays, just use the JSON method!

 

Congratulations, now you have learned how to work with the HTTP Requests using the Requests library and use Python for Delphi to display it in the Delphi Windows GUI app! Now you can try more advanced operations using Requests library and Python4Delphi.

Check out the Requests library for Python and use it in your projects: https://pypi.org/project/requests/ and

Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: https://github.com/pyscripter/python4delphi

close
Related posts
Learn PythonPythonPython GUIVideos

How To Make Your Data Visualization Effective

DelphiDelphiVCLPythonPython GUIWindows

This Is A List Of All Routines And Classes in the VCL

DelphiDelphiVCLPythonPython GUIWindows

What is DelphiVCL.Application.CreateHandle?

DelphiDelphiVCLPythonPython GUIWindows

Creating A New Application With DelphiVCL.Application.Create

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

fr_FRFrench