Flask返回发布了JSON

时间:2017-02-18 12:30:43

标签: python json flask

我的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。我该怎么做呢?

1 个答案:

答案 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)