Python:
from flask import Blueprint, request, make_response, jsonify
index_page = Blueprint("index_page", __name__)
@index_page.route("/")
def hello():
return "I Love jarvisyqliu"
@index_page.route("/my/<username>")
def my(username):
return "I Love test %s" % username
@index_page.route("/get")
def get():
req = request.values
var_a = req['a'] if "a" in req else None
return "request:%s,params:%s,var_a:%s" % (request.method, request.args, var_a)
@index_page.route("/post", methods=["POST"])
def post():
req = request.values
var_a = req['a'] if "a" in req else None
return "request:%s,params:%s,var_a:%s" % (request.method, request.form, var_a)
@index_page.route("/upload", methods=["POST"])
def upload():
f = request.files['file'] if "file" in request.files else None
return "request:%s,params:%s,file:%s" % (request.method, request.files, f)
@index_page.route("/text")
def text():
return "text/html"
@index_page.route("/text_same")
def text_same():
response = make_response("text/html", 200)
return response
@index_page.route("/json")
def json():
import json
data = {"a": "b"}
response = make_response(json.dumps(data))
response.headers["Content-Type"] = "application/json"
return response
@index_page.route("/json_same")
def json_same():
data = {"a": "b"}
response = make_response(jsonify(data))
return response
@index_page.route("/template")
def template():
return render_template("index.html")
PS:jsonify会自动构建Content-Type为application/json文章来源地址https://www.toymoban.com/news/detail-692672.html
文章来源:https://www.toymoban.com/news/detail-692672.html
到了这里,关于Flask对请求进行多个格式的响应的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!