如何使用 aiohttp 的 ClientSession 设置自定义 HTTP 请求头部?
- 内容介绍
- 文章标签
- 相关推荐
本文共计233个文字,预计阅读时间需要1分钟。
前言:ClientSession是拥有客户端API操作的核心和主要入口点。会话包含cookie、存储和连接池,因此cookie和连接在同一个会话中发送的HTTP请求之间共享。自定义请求头部,如果您需要将HTTP请求包含在内。
前言
ClientSession是所有客户端 API 操作的核心和主要入口点。会话包含 cookie 存储和连接池,因此 cookie 和连接在同一会话发送的 HTTP 请求之间共享。
自定义请求头部
如果您需要将 HTTP 标头添加到请求中,请将它们传递给 headers 参数。如在请求头部添加"Content-Type": "application/json"
headers = {"Content-Type": "application/json"
}
完整代码:
headers = {
"Content-Type": "application/json"
}
body = {
"username": "test",
"password": "123456"
}
async with session.post(
url=url, json=body, headers=headers) as resp:
print(resp.status)
res = await resp.text()
print(res)
async def main():
async with aio127.0.0.1:8000"
async with aiowww.558idc.com/aqt.html欢迎留下您的宝贵建议】
本文共计233个文字,预计阅读时间需要1分钟。
前言:ClientSession是拥有客户端API操作的核心和主要入口点。会话包含cookie、存储和连接池,因此cookie和连接在同一个会话中发送的HTTP请求之间共享。自定义请求头部,如果您需要将HTTP请求包含在内。
前言
ClientSession是所有客户端 API 操作的核心和主要入口点。会话包含 cookie 存储和连接池,因此 cookie 和连接在同一会话发送的 HTTP 请求之间共享。
自定义请求头部
如果您需要将 HTTP 标头添加到请求中,请将它们传递给 headers 参数。如在请求头部添加"Content-Type": "application/json"
headers = {"Content-Type": "application/json"
}
完整代码:
headers = {
"Content-Type": "application/json"
}
body = {
"username": "test",
"password": "123456"
}
async with session.post(
url=url, json=body, headers=headers) as resp:
print(resp.status)
res = await resp.text()
print(res)
async def main():
async with aio127.0.0.1:8000"
async with aiowww.558idc.com/aqt.html欢迎留下您的宝贵建议】

