news 2026/8/11 16:17:16

requests.post(url,json,headers,timeout)函数参数json、data、parameter的区别

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
requests.post(url,json,headers,timeout)函数参数json、data、parameter的区别

这是一个非常常见的 requests 库使用问题。下面详细解释一下这三个参数的区别和使用场景:

1.json参数

使用场景:当你要发送 JSON 格式的数据到服务器时

特点

  • 自动将 Python 对象(dict、list 等)序列化为 JSON 字符串

  • 自动设置Content-Typeapplication/json

  • 适用于 RESTful API

import requests payload = { "name": "John", "age": 30, "hobbies": ["reading", "swimming"] } # 推荐方式 - 使用 json 参数 response = requests.post( url="https://api.example.com/users", json=payload, # 自动序列化为 JSON headers=headers, timeout=30 ) # 等价于手动处理: # headers['Content-Type'] = 'application/json' # data = json.dumps(payload) # requests.post(url, data=data, headers=headers)

2.data参数

使用场景

  • 发送表单数据(application/x-www-form-urlencoded)

  • 发送 multipart 表单数据(文件上传)

  • 发送原始字符串数据

# 场景1:表单数据 form_data = { "username": "john", "password": "secret" } response = requests.post( url="https://example.com/login", data=form_data, # 自动编码为表单格式 headers=headers ) # 场景2:文件上传 files = {'file': open('image.jpg', 'rb')} response = requests.post( url="https://example.com/upload", files=files, data={'description': 'My image'} # 其他表单字段 ) # 场景3:原始字符串 xml_data = "<user><name>John</name></user>" response = requests.post( url="https://example.com/api", data=xml_data, headers={'Content-Type': 'application/xml'} )

3.params参数

使用场景:用于 URL 查询字符串参数(GET 请求的参数)

特点

  • 参数会附加在 URL 的?后面

  • 只用于 GET 请求或需要在 URL 中传递参数的情况

query_params = { "page": 1, "limit": 10, "search": "python" } response = requests.get( url="https://api.example.com/articles", params=query_params, # 添加到 URL: ?page=1&limit=10&search=python headers=headers )

总结对比

参数用途Content-Type位置
json发送 JSON 数据application/json请求体
data发送表单数据/原始数据多种类型请求体
paramsURL 查询参数-URL 中

实际使用建议

import requests # ✅ 推荐:RESTful API 使用 json api_response = requests.post( "https://api.example.com/users", json={"name": "John", "age": 30}, headers={"Authorization": "Bearer token"} ) # ✅ 推荐:表单提交使用 data form_response = requests.post( "https://example.com/login", data={"username": "john", "password": "secret"} ) # ✅ 推荐:GET 请求参数使用 params search_response = requests.get( "https://api.example.com/search", params={"q": "python", "page": 1} ) # ❌ 避免:不要混淆使用 # 错误:用 data 发送 dict 到期望 JSON 的 API # requests.post(url, data=dict_data) # 可能出错 # 正确:明确指定数据格式 requests.post(url, json=dict_data) # 对于 JSON API requests.post(url, data=dict_data) # 对于表单接口

简单记忆

  • API 交互→ 用json

  • 表单提交→ 用data

  • URL 参数→ 用params

  • 文件上传→ 用files+data

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/11 16:16:35

5分钟免费解锁Office高级功能:Ohook开源工具的完整指南

5分钟免费解锁Office高级功能&#xff1a;Ohook开源工具的完整指南 【免费下载链接】ohook An universal Office "activation" hook with main focus of enabling full functionality of subscription editions 项目地址: https://gitcode.com/gh_mirrors/oh/ohook…

作者头像 李华
网站建设 2026/8/11 16:16:14

FDE一天到底在干什么——前沿部署工程师的真实工作内容拆解

# FDE一天到底在干什么——前沿部署工程师的真实工作内容拆解## 引言前面那篇讲了FDE前沿部署工程师是个什么岗位&#xff0c;回答了"企业为什么急着招这类人"。但很多人对FDE的认知还停留在概念层面——知道这是个新岗位&#xff0c;知道跟AI有关&#xff0c;但不知…

作者头像 李华
网站建设 2026/8/11 16:16:03

5个实用技巧彻底解锁Wand专业版功能:告别时间限制的终极指南

5个实用技巧彻底解锁Wand专业版功能&#xff1a;告别时间限制的终极指南 【免费下载链接】Wand-Enhancer Advanced UX and interoperability extension for Wand (WeMod) app 项目地址: https://gitcode.com/GitHub_Trending/we/Wand-Enhancer Wand-Enhancer是一款专为W…

作者头像 李华
网站建设 2026/8/11 16:15:12

2026最新:苹果用户怎么选语音转文字?3款实用免费工具亲测推荐

先回答用户真正关心的问题 2026年苹果用户选免费语音转文字工具&#xff0c;可直接根据核心需求匹配方案&#xff1a;仅需基础短录音转写可选用录音转文字助手&#xff0c;需要专业音频处理配合转写可选Adobe Audition&#xff0c;需要转写自动AI纪要整理的办公学习用户&#…

作者头像 李华