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 Demo01 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute script button, and get the result in the upper Memo. You can find the Demo01 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


References & further readings

[1] PyPI (Python Package Index). (2024). 

requests 2.32.3: Python HTTP for Humans. Python Software Foundation. pypi.org/project/requests

[2] Reitz, K. (2024). 

Quickstart. Requests documentation. docs.python-requests.org/en/latest/user/quickstart

[3] Reitz, K. (2024). 

Requests: HTTP for Humans™. Requests documentation. docs.python-requests.org/en/latest

[4] Wootten, I. (2022). 

The best Python HTTP clients. ScrapingBee blog. scrapingbee.com/blog/best-python-http-clients

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

CodeIDELearn PythonPythonPython GUITkinter

How To Make 20 ChatGPT Prompts Work With Python GUI Builders And Matplotlib Library?

CodeIDELearn PythonPythonPython GUITkinter

How To Make 20 ChatGPT Prompts Work With Python GUI Builders And Pillow Library?

Leave a Reply

Your email address will not be published. Required fields are marked *