我的Flask路线定义如下:
# main.py
from flask import Blueprint, request
main = Blueprint('main',__name__)
@main.route("/")
def hello():
return "Hello World!"
@main.route("/keke/")
def keke():
return "Hello Keke!"
@main.route("/upload/", methods=['POST'])
def upload():
if request.json:
return request.json
upload
路由会收到已发布的JSON。我想返回那个JSON,所以我可以检查内容是否到达服务器OK。但是,行return request.json
会引发错误TypeError: 'dict' object is not callable
。我该怎么做呢?
答案 0 :(得分:3)
request.json
是已解码的Python对象。使用jsonify()
function将其重新转换为JSON响应:
from flask import jsonify
@main.route("/upload/", methods=['POST'])
def upload():
if request.json:
return jsonify(request.json)