0

I have my Flask routes defined like so:

# 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

The upload route receives a JSON that is posted. I would like to return that JSON back so I can check that the contents arrived at the server OK. However the line return request.json throws the error TypeError: 'dict' object is not callable. How would I go about doing this?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Kex
  • 8,023
  • 9
  • 56
  • 129

1 Answers1

4

request.json is the decoded Python object. Use the jsonify() function to turn that back into a JSON response:

from flask import jsonify

@main.route("/upload/", methods=['POST'])
def upload():
    if request.json:
        return jsonify(request.json)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343