Python HTTPS Requests Using requests Module
Introduction
The requests module in Python is the most popular and user-friendly HTTP library used to send HTTPS requests, consume REST APIs, and perform web data fetching. It simplifies complex HTTP calls into a few lines of code.
Installation of requests Module
If not already installed, use pip:
pip install requests
Common HTTPS Methods in Python Using requests
Method | Purpose |
---|---|
GET | Retrieve data |
POST | Send data to the server |
PUT | Update existing data |
DELETE | Delete a resource |
HEAD | Retrieve headers |
PATCH | Partially update data |
1. Python requests.get() Example
import requests
response = requests.get('https://api.github.com')
print("Status Code:", response.status_code)
print("Headers:", response.headers['Content-Type'])
print("Content:", response.text[:100]) # Limit output
Output:
Status Code: 200 Headers: application/json; charset=utf-8 Content: { "current_user_url": "https://api.github.com/user", ...
2. Sending Query Parameters
payload = {'q': 'python requests', 'sort': 'stars'}
r = requests.get('https://api.github.com/search/repositories', params=payload)
print(r.url)
Output:
https://api.github.com/search/repositories?q=python+requests&sort=stars
3. Python requests.post() Example
url = 'https://httpbin.org/post'
data = {'username': 'admin', 'password': '123'}
response = requests.post(url, data=data)
print(response.json())
Output:
{ "form": { "password": "123", "username": "admin" }, ...
4. Sending JSON Data
import json
url = 'https://httpbin.org/post'
json_data = {'id': 1, 'title': 'Learn Python'}
response = requests.post(url, json=json_data)
print(response.json()['json'])
Output:
{'id': 1, 'title': 'Learn Python'}
5. Sending Custom Headers
headers = {'User-Agent': 'MyApp/1.0'}
response = requests.get('https://httpbin.org/headers', headers=headers)
print(response.json())
6. Handling HTTPS Response Status Codes
r = requests.get('https://api.github.com/invalid-url')
if r.status_code == 404:
print("Page not found!")
Output:
Page not found!
7. Timeout and Error Handling
try:
r = requests.get('https://httpbin.org/delay/5', timeout=2)
except requests.exceptions.Timeout:
print("Request timed out")
8. Basic Authentication
from requests.auth import HTTPBasicAuth
response = requests.get('https://httpbin.org/basic-auth/admin/123', auth=HTTPBasicAuth('admin', '123'))
print(response.status_code)
Output:
200
9. Downloading a File Over HTTPS
url = 'https://www.example.com/sample.pdf'
response = requests.get(url)
with open('sample.pdf', 'wb') as file:
file.write(response.content)
print("File downloaded.")
HTTPS Security Note
The requests module automatically verifies SSL certificates. To disable it (not recommended):
requests.get('https://example.com', verify=False)