Learn To Work With HTTP Requests Using Python Requests Library In A Delphi Windows App
February 24, 2021 By Muhammad Azizul Hakim
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?
Для просмотра ссылки Войдиили Зарегистрируйся or P4D with Для просмотра ссылки Войди или Зарегистрируйся 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 Для просмотра ссылки Войдиили Зарегистрируйся. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this Для просмотра ссылки Войди или Зарегистрируйся.
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:
Different types of input data are also possible. For example, dictionaries, tuples, lists:
The results
The response in Python4Delphi:
Let’s try more interesting example using GitHub API:
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 Для просмотра ссылки Войдиили Зарегистрируйся 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 Для просмотра ссылки Войдиили Зарегистрируйся operations using Requests library and Python4Delphi.
Check out the Requests library for Python and use it in your projects: Для просмотра ссылки Войдиили Зарегистрируйся and
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: Для просмотра ссылки Войдиили Зарегистрируйся
February 24, 2021 By Muhammad Azizul Hakim
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?
Для просмотра ссылки Войди
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 Для просмотра ссылки Войди
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:
Python:
import requests
r = requests.get('https://example.com')
print(r.text)
print(r.headers)
print(r.status_code)
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:
Python:
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)
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.
Python:
import requests
from getpass import getpass
r=requests.get('https://test.org', auth=('username', getpass()))
print(r.status_code)
Let’s try more interesting example using GitHub API:
Python:
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())
If this is your first time using GitHub API and you don’t know how to generate your access token, this is the Для просмотра ссылки Войди
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 Для просмотра ссылки Войди
Check out the Requests library for Python and use it in your projects: Для просмотра ссылки Войди
Check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi: Для просмотра ссылки Войди