Master Python Requests: GET, POST, HTTPS, File Transfer, Auth & Test Automation
This guide demonstrates how to use Python's requests library for sending GET, POST, and HTTPS requests, handling file uploads and downloads, managing timeouts, performing authentication, session and token handling, generating signatures, and organizing a complete API test automation framework.
1. Sending GET request
Import requests, define the URL, payload and headers, then call requests.get and print the response text, encoding, and decoded text.
import requests
url = "http://xxxxxxx"
payload = "{\"head\":{\"accessToken\":\"\",\"lastnotice\":0,\"msgid\":\"\"},\"body\":{\"user_name\":\"super_admin\",\"password\":\"b50c34503a97e7d0d44c38f72d2e91ad\",\"role_type\":1}}"
headers = {
'Content-Type': 'text/plain',
'Cookie': 'akpsysessionid=bafc0ad457d5a99f3a4e53a1d4b32519'
}
r = requests.get(url=url, headers=headers, data=payload)
print(r.text)
print(r.encoding)
print(r.text.encode('utf-8').decode('unicode_escape'))2. Sending POST request
Similar to GET, but using requests.post to send the payload.
import requests
url = "http://xxxxxxx"
payload = "{\"head\":{\"accessToken\":\"\",\"lastnotice\":0,\"msgid\":\"\"},\"body\":{\"user_name\":\"super_admin\",\"password\":\"b50c34503a97e7d0d44c38f72d2e91ad\",\"role_type\":1}}"
headers = {
'Content-Type': 'text/plain',
'Cookie': 'akpsysessionid=bafc0ad457d5a99f3a4e53a1d4b32519'
}
r = requests.post(url=url, headers=headers, data=payload)
print(r.text)3. Sending HTTPS request
Demonstrates disabling SSL verification or providing a certificate path.
import requests
url = 'https://www.ctrip.com/'
# Disable verification
r = requests.post(url=url, verify=False)
# Or specify certificate path
r = requests.post(url=url, verify='path/to/cert')
print(r.text)4. File upload
Upload a file using requests.post with a file dictionary.
import requests
file = {'filename': open('file_name', 'rb')}
response = requests.post('http://example.com/upload', files=file)
print(response.text)5. File download
Download small and large files, writing the content to disk.
# Small file
import requests
r = requests.get('https://img.sitven.cn/Tencent_blog_detail.jpg')
with open('D:/a.jpg', 'wb') as f:
f.write(r.content)
# Large file with streaming
import requests
def download(url, file_path):
s = requests.session()
r = s.get(url, stream=True, verify=False)
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=512):
f.write(chunk)
if __name__ == '__main__':
download('https://www.url.com/test/export', 'D:/a.xlsx')6. Timeout handling
Loop with retries and a very short timeout to trigger errors.
import requests
for i in range(10):
try:
url = "http://example.com"
data = {"head": {"lastnotice":0,"msgid":"","accessToken":"89a08bff-..."}, "body": {"clinicid":"978f66..."}}
r = requests.post(url=url, json=data, timeout=0.03)
print(r.text)
print(r.cookies)
except Exception:
print('error')7. Authentication and session management
Basic auth, session reuse, token handling, and signature generation.
# Basic auth
import requests
url = 'http://192.168.1.1'
r = requests.get(url, auth=('admin', '123456'), timeout=10)
print(r.text)
# Session usage
session = requests.session()
response = session.post(url, headers=req_header, data=form_data)
# Token handling (login then reuse token)
import requests
url = 'http://example.com/login'
json = {"head": {"accessToken":"","lastnotice":0,"msgid":""}, "body": {"username":"15623720880","password":"48028d...","forceLogin":0}}
r = requests.post(url=url, json=json)
print(r.text)
print(r.cookies)
# Use token in subsequent request
for i in range(1):
try:
url = 'http://example.com/next'
data = {"head": {"lastnotice":0,"msgid":"","accessToken":"89a08bff-..."}, "body": {...}}
r = requests.post(url=url, json=data, timeout=0.09)
print(r.text)
print(r.cookies)
except Exception:
print('error')8. Signature generation (WeChat Pay example)
import hashlib
stringA = "appid=wxd930ea5d5a258f4f&body=test&device_info=1000&mch_id=10000100&nonce_str=ibuaiVcKdpRxkhJA"
md = hashlib.md5()
md.update(stringA.encode())
AES = md.hexdigest().upper()
print(AES)9. Automation project structure
Typical layout for a Python API test framework.
config : configuration files (e.g., server IP, database settings)
common : reusable utility functions (Excel reading, SQL execution)
testdata : test data files (xls, txt, csv)
test_case : test case scripts
report : generated HTML/XML reports
run_case : test runner script
log : logging output
9.1 Config module
def server_ip():
server_add = {
'dev_ip': 'http://his.dev.com',
'sit_ip': 'http://his.sit.com'
}
return server_add['dev_ip']
def sql_conf():
host = 'localhost'
user = 'root'
password = '123456'
database = 'mysql'
port = 3306
charset = 'utf8'
return host, user, password, database, port, charset9.2 Common utilities
# Read Excel data
import xlrd
def get_excel_value(i):
filename = r"../testdata/jiekou.xls"
book = xlrd.open_workbook(filename)
sheet = book.sheet_by_index(0)
return sheet.cell_value(i,1), sheet.cell_value(i,2)
# Execute SQL and return results
import pymysql
from config.sql_conf import *
def get_sql(sql):
host, user, password, database, port, charset = sql_conf()
db = pymysql.connect(host=host, user=user, password=password, database=database, port=port, charset=charset)
cursor = db.cursor()
cursor.execute(sql)
data = cursor.fetchall()
cursor.close()
db.close()
return data9.3 Test data
Stores xls, txt, csv files used by test cases.
9.4 Test cases
from common.get_mysql import get_sql
from config.cof import server_ip
from common.get_excel import *
from config.sql_conf import *
import requests
def test_aokao_login():
url = server_ip() + '/service/user/login'
username, password = get_excel_value(1)
json = {
"head": {"accessToken":"","lastnotice":0,"msgid":""},
"body": {"username":username,"password":password,"forceLogin":0}
}
r = requests.post(url=url, json=json)
print(r.text)
assert r.status_code == 200
assert '"accessToken":"89a08bff-15d7-4d7a-9967-0b5f4fb699ce"' in r.text
def test_aokao_registadd():
url = server_ip() + '/service/registration/registadd'
data = {"head": {"lastnotice":0,"msgid":"","accessToken":"89a08bff-..."}, "body": {...}}
r = requests.post(url=url, json=data, timeout=0.09)
print(r.text)
print(r.cookies)
assert r.status_code == 2009.5 Report generation
HTML and XML reports are stored in the report directory.
9.6 Running test cases
import pytest
if __name__ == "__main__":
pytest.main(['../test_case/', '--html=../report/report.html', '--junitxml=../report/report.xml'])© Article originally published on Jianshu . All rights reserved by the original author.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
