理解 HTTP POST 请求中的 json 和 data 参数

在使用 Python 发送 HTTP POST 请求时(无论是使用 requests 还是 aiohttp),json 和 data 参数有明确的区别和使用场景。理解这些区别对正确构建请求至关重要。

关键区别

特性 json 参数 data 参数
内容类型 自动设置为 application/json 需要手动设置(默认是 application/x-www-form-urlencoded)
数据处理 自动序列化 Python 对象为 JSON 需要手动序列化
使用场景 API 请求(JSON-RPC, RESTful API) 表单提交、文件上传、自定义格式
编码 UTF-8 取决于内容类型
易用性 更简单(自动处理) 需要更多手动工作
  1. 何时使用 data

参数: 使用场景:

  1. 表单提交(HTML 表单数据):

    python

    复制代码
    form_data = {"username": "john", "password": "secret"}
    response = requests.post("https://example.com/login", data=form_data)
  2. 文件上传:

    python

    复制代码
    files = {"file": open("report.pdf", "rb")}
    response = requests.post("https://example.com/upload", files=files)
  3. 发送原始文本(非 JSON):

    python

    复制代码
    xml_data = "<user><name>John</name></user>"
    response = requests.post("https://example.com/api", 
                            data=xml_data,
                            headers={"Content-Type": "application/xml"})
  4. 多部分表单数据:

    python

    复制代码
    from aiohttp import FormData
    
    form = FormData()
    form.add_field("username", "john")
    form.add_field("avatar", open("avatar.jpg", "rb"), 
                  filename="avatar.jpg",
                  content_type="image/jpeg")
    
    async with session.post(url, data=form) as response:
  5. 自定义内容类型:

    python

    复制代码
    custom_data = "custom format data"
    response = requests.post("https://example.com/api", 
                            data=custom_data,
                            headers={"Content-Type": "text/plain"})

2. 何时使用 json 参数

使用场景:

  • 当 API 期望接收 JSON 格式的数据时(大多数现代 API 使用 JSON)

  • 当需要发送结构化数据(字典、列表等)

  • 当 API 文档指定请求体应为 JSON

示例:

python

复制代码
import requests

data = {"name": "John", "age": 30}
response = requests.post("https://api.example.com/users", json=data)

在 aiohttp 中:

python

复制代码
async with session.post(url, json=params) as response:

优点:

  1. 自动设置 Content-Type: application/json 头

  2. 自动将 Python 对象序列化为 JSON

  3. 简化代码

3. 最佳实践建议

  1. 推荐使用 json=params:

    python

    复制代码
    response = requests.post(url, headers=headers, json=params, timeout=60)
    • 更简洁

    • 更安全(自动处理序列化)

    • 自动设置正确的 Content-Type

  2. 如果使用 data,必须手动序列化:

    python

    复制代码
    response = requests.post(url, headers=headers, data=json.dumps(params), timeout=60)
    • 确保传递的是字符串,不是字典, 手动将`params`字典序列化为JSON字符串,然后通过`data`参数发送

    • 确保设置了正确的 Content-Type

  3. 避免使用 data=params:

    • 这总是错误的,除非您确实想发送表单数据, 这会将`params`字典编码为`application/x-www-form-urlencoded`格式(即表单数据),而不是JSON。

    • 对于 JSON API,会导致服务器解析错误

相关推荐
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
A黄俊辉A3 天前
uniapp webview中实现 app和内嵌的H5双向通信
vue.js·json
XUEYUAN52123 天前
ASN 自治系统号风控:平台如何通过 IP 所属自治域批量识别代理流量
python·网络协议·http·网络安全·socks5
CHENKONG_CK3 天前
破解制鞋打磨痛点:RFID赋能去毛刺工序自动化升级
网络·单片机·嵌入式硬件·网络协议·tcp/ip
伞伞悦读3 天前
【第37期】Python JSON 与配置详解:序列化、反序列化、嵌套结构和配置文件
开发语言·python·json
Mikko73 天前
jackson-databind 升到 2.21.6 就安全了吗?jackson-core 是另一个坐标,它那条 high 全局库至今没收
java·后端·安全·json
z落落3 天前
C#UDP+串口服务端+UDP 客户端(含 CRC16 校验)
网络·网络协议·udp
曼巴UE53 天前
UE5 客户端 需要的网络同步概念总结(3 )-事件同步 RPC 以及三种调用方式
网络·c++·网络协议·学习·rpc·ue5
captain3763 天前
HTTP(2)
java·网络·http·java-ee
开开心心就好3 天前
安卓手写文字生成工具,多种纸张一直免费
网络·网络协议·tcp/ip·leetcode·智能手机·电脑·模拟退火算法