6.Requests【接口请求】requests模拟发送post请求
一、前言
上节课我们讲到了如何通过requests模块模拟发送get请求,我们这节课学习一下另一个常见的http请求方式:post请求。与get不同的是,post请求可以传请求体,而get没有请求体。下面我们对如何发送一个简单的post请求作讲解。
二、学习目标
1.简单的post请求
三、知识点
1.【简单的post请求】
源码:
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request('post', url, data=data, json=json, **kwargs)
-
语法:
requests.post(url, data=None, json=None, **kwargs)
-
参数:
url(必填参数):接口的请求地址。
data(选填参数):接口的请求体,传入的参数可以是字典格式(推荐),也可以是字符串。
json(选填参数):接口的请求体,传入的参数可以是字典格式(推荐),也可以是字符串。
**kwargs(选填,不定长参数):代表还可以传其他参数,如headers,cookies等。
-
返回值:
响应对象
-
示例:
import requests res = requests.post('http://www.httpbin.org/post') print(res.text) #text是响应对象的属性,指响应体的文本内容
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?