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
  • 107,083
  • 22
  • 201
  • 376
Kex
  • 7,245
  • 8
  • 48
  • 109

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
  • 963,270
  • 265
  • 3,804
  • 3,187