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.
Table of Contents
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:
1 2 3 4 5 6 7 |
import requests r = requests.get('https://example.com') print(r.text) print(r.headers) print(r.status_code) |
The result in Python4Delphi GUI:
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import requests payload1 = {'key1': 'value1', 'key2': 'value2'} r = requests.post("https://httpbin.org/post", data=payload1) print(r.text) payload2 = [('key1', 'value1'), ('key1', 'value2')] r1 = requests.post('https://httpbin.org/post', data=payload2) print(r1.text) payload3 = {'key1': ['value1', 'value2']} r2 = requests.post('https://httpbin.org/post', data=payload3) print(r2.text) |
The results:
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
.
1 2 3 4 5 |
import requests from getpass import getpass r=requests.get('https://test.org', auth=('username', getpass())) print(r.status_code) |
The response in Python4Delphi:
Let’s try more interesting example using GitHub API:
1 2 3 4 5 6 7 8 9 |
import requests r = requests.get('https://api.github.com/users', auth=('your username', 'your personal access token')) print(r.status_code) print(r.headers['content-type']) print(r.encoding) print(r.text) print(r.json()) |
Let’s see the results:
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