接口请求常见get和post传递参数
如果是传递得json格式得参数:
p={"name":"john","age":17}
response=requests.post("http://localhost:5000/post_params",json=p)
print(response.text)
###输出:
{"name": "john", "age": 17}
如果是以html中form得形式传递参数
response=requests.post("http://localhost:5000/post_params",data=p)
print(response.text)
如果form传递得是json格式需要以下面得方式
import json
response=requests.post("http://localhost:5000/post_params",data=json.dumps(p))
print(response.text)
说明:
需要将字典行驶得参数转换为json格式,再进行传递
1.get请求传递参数
params={"name":"john","age":17}
response=requests.get("http://localhost:5000/get_params",params=params)
print(response.text)
print(response.url)
##输出
{"name": "john", "age": "17"}
http://localhost:5000/get_params?name=john&age=17
说明:
这里做了三件事,先定义一个参数字典p,然后将参数字典以params参数传递给get方法,最后将响应信息打印出来,从响应信息可以看到参数被正确得传递进去,requests帮我们把参数组合到了url里面
2.post请求传递参数
如果是传递得json格式得参数
p={"name":"john","age":17}
response=requests.post("http://localhost:5000/post_params",json=p)
print(response.text)
###输出:
{"name": "john", "age": 17}
如果是以html中form得形式传递参数
response=requests.post("http://localhost:5000/post_params",data=p)
print(response.text)
如果form传递得是json格式需要以下面得方式
import json
response=requests.post("http://localhost:5000/post_params",data=json.dumps(p))
print(response.text)
3.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现