Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

python requests.post

作者:惨美歌   发布日期:2025-04-09   浏览:85

import requests

# 定义发送POST请求的URL
url = 'https://httpbin.org/post'

# 定义要发送的数据,可以是字典、列表或多部分编码的文件等
data = {'key': 'value'}

# 发送POST请求
response = requests.post(url, data=data)

# 打印响应内容
print(response.text)

解释说明:

  1. 导入requests库import requests 用于发送HTTP请求。
  2. 定义URLurl = 'https://httpbin.org/post' 是目标服务器的URL,这里使用的是一个测试用的公共API。
  3. 定义数据data = {'key': 'value'} 是要发送的表单数据,以字典形式表示。
  4. 发送POST请求response = requests.post(url, data=data) 使用 requests.post() 方法发送POST请求,并将返回的响应存储在 response 变量中。
  5. 打印响应内容print(response.text) 输出服务器返回的响应内容。

如果需要发送JSON数据,可以使用 json 参数代替 data 参数:

import requests

url = 'https://httpbin.org/post'
json_data = {'key': 'value'}

response = requests.post(url, json=json_data)
print(response.text)

这种方式会自动将字典转换为JSON格式并设置正确的 Content-Type 头。

上一篇:python print不换行

下一篇:python dict update

大家都在看

python时间格式

python ord和chr

python中的yield

python list.pop

python的for i in range

npm config set python

python代码简单

python读取文件夹

python中turtle

python 输出时间

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站