最简单的实验室资产管理系统,使用Flask,mysql,html(四、知识补充)

一、get请求和post请求的区别

  • GET请求的数据放在URL里,读取用 request.args
python 复制代码
http://127.0.0.1:5000/get-example?name=ChatGPT
  • POST请求的数据放在请求体里,读取用 request.json
python 复制代码
{"name": "ChatGPT"}

二、get请求和post请求的示例

1.服务器代码

python 复制代码
from flask import Flask, request, jsonify

app = Flask(__name__)

@app.route('/get-example', methods=['GET'])
def get_example():
    # 从URL参数中获取name参数
    name = request.args.get('name', 'default_name')
    return jsonify({"you_sent_name": name})

@app.route('/post-example', methods=['POST'])
def post_example():
    # 从POST请求的JSON体中获取数据
    data = request.json
    return jsonify({"you_sent": data})

if __name__ == "__main__":
    app.run(debug=True)

2.客户端代码

python 复制代码
import requests

# GET请求
get_url = "http://127.0.0.1:5000/get-example?name=ChatGPT"
response_get = requests.get(get_url)
print("GET请求状态码:", response_get.status_code)
print("GET请求响应:", response_get.json())

# POST请求
post_url = "http://127.0.0.1:5000/post-example"
data = {"name": "ChatGPT", "task": "test POST"}
response_post = requests.post(post_url, json=data)
print("POST请求状态码:", response_post.status_code)
print("POST请求响应:", response_post.json())

3.运行结果

相关推荐
别抢我的锅包肉4 小时前
【MySQL】第四节 - 多表查询、多表关系全解析
数据库·mysql·datagrip
zzh0815 小时前
MySQL高可用集群笔记
数据库·笔记·mysql
Shely20175 小时前
MySQL数据表管理
数据库·mysql
寂夜了无痕6 小时前
MySQL 主从延迟全链路根因诊断与破局法则
数据库·mysql·mysql主从延迟
爱丽_6 小时前
分页为什么越翻越慢:offset 陷阱、seek 分页与索引排序优化
数据库·mysql
Bat U6 小时前
MySQL数据库|表设计+新增+分组查询
数据库·mysql
qing222222227 小时前
Linux中修改mysql数据表
linux·运维·mysql
J2虾虾7 小时前
MySQL的基本操作
数据库·mysql
2601_949815338 小时前
MySQL输入密码后闪退?
数据库·mysql·adb
_下雨天.8 小时前
MySQL高可用
数据库·mysql