Python 用 post 提交数据时如何提交原始数据?
直接抓包提交的数据时 Ptfmx6PQ==
用 python requests.post 提交数据后 就自动 url 编码为 Ptfmx6PQ%3D%3D
Python 用 post 提交数据时如何提交原始数据?
5 回复
无所谓啊,等收到之后就解码了
在Python里用POST提交原始数据,直接用requests库的data参数就行。别用json参数,那个会自动序列化。关键是把你的数据转成字节串,然后手动设置Content-Type头。
比如你要提交一段纯文本或者自定义格式的数据:
import requests
url = 'https://httpbin.org/post'
raw_data = '这是一段原始文本数据,也可以是XML、自定义格式等'
response = requests.post(
url,
data=raw_data.encode('utf-8'), # 关键:将字符串编码为字节
headers={'Content-Type': 'text/plain'} # 根据你的数据类型设置
)
print(response.status_code)
print(response.text)
核心就两步:
- 把你的数据(字符串)用
.encode()转成字节。 - 在
headers里明确告诉服务器你发的是什么类型,比如text/plain、application/xml或者application/octet-stream。
如果数据本来就是字节(比如文件读取的bytes),直接传就行:
with open('file.bin', 'rb') as f:
raw_bytes = f.read()
response = requests.post(url, data=raw_bytes, headers={'Content-Type': 'application/octet-stream'})
简单总结:用data参数传字节,配好Content-Type头。
from urllib.parse import unquote
url = unquote(url)
在服务器端解码
Form 会编码,需要后端解码,改用 JSON 提交就好了

