Backend Development 6 min read

Automating API Tests for Special Character Handling with Python Requests

This article explains how to use Python's requests library and CSV handling to automate API testing of payment interfaces, focusing on correctly processing special characters in usernames and account names to ensure secure and fast transactions.

FunTester
FunTester
FunTester
Automating API Tests for Special Character Handling with Python Requests

In fast‑moving internet product cycles, regression testing time is severely compressed, making it crucial to verify that payment and settlement APIs correctly handle special characters in usernames and account names, especially in finance and other critical applications.

Python, with its portability and rich third‑party libraries, is an efficient tool for API automation; the typical workflow includes assembling the request (URL, headers, data), sending it (GET or POST), and parsing the response.

The requests library simplifies these steps. After installing it, a simple GET request can be made:

#导入requests包
import requests
#接口URL,这里以一个虚拟网址为例
url = ‘http://test.org’
response = requests.get(url)
print(response.txt)

A POST request with data is similar:

import requests 
url = "http://test.org/post"
data = '''
        "name": "张三",
        "age": 18
        }'''
response = requests.post(url=url, data=data) 
print(response.text)

To test edge cases such as special characters, the script reads a CSV file containing a character set and injects each character into the request payload, automating coverage of rare or non‑ASCII names.

import csv
with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    print(type(reader))
for row in reader:
    print(row)

In a remittance scenario, the article builds a data dictionary, loads the special‑character list, iterates over it, replaces the sender's account name with each character, sends a POST request, and prints the response:

import requests, csv
url = ‘http://kuahangzhuanzhang.org/post’
#构造输入数据
data = {
“sendaccnm”: “王小林”,
“sendaccno”: “1234567890”,
“sendbkno”: “103100000026”,
“recvaccnm”: “马小腾”,
“recvaccno”: “2345678901”,
“recvbkno”: “105100000017”
}
#定义列表,读取特殊字符集文件,列表储存
li = []
with open('data.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
         li.append(row[0])
#替换data中的数据,产生带有特殊字符的户名数据并发起请求
for i in range(len(li)):
    new_accnm = data.get("sendaccnm").replace(data.get("sendaccnm")[1],li[i])
    data["sendaccnm"] = new_accnm
    print(data)    #可打印一下替换后的数据
response = requests.post(url=url, data=data)
print(response.text)    #打印响应数据

After execution, the original account names are replaced with versions containing rare characters, minority names, or foreign names, demonstrating effective automated testing for special‑character handling in financial APIs.

The article is published by the FunTester public account, which shares testing‑related articles and resources.

PythonautomationCSVAPI testingRequestsspecial characters
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.