votes up 3

jsonify() behavior undefined when passed both args and kwargs

Package:
flask
github stars 56479
Exception Class:
TypeError

Raise code

    separators = (",", ":")

    if current_app.config["JSONIFY_PRETTYPRINT_REGULAR"] or current_app.debug:
        indent = 2
        separators = (", ", ": ")

    if args and kwargs:
        raise TypeError("jsonify() behavior undefined when passed both args and kwargs")
    elif len(args) == 1:  # single args are passed directly to dumps()
        data = args[0]
    else:
        data = args or kwargs

    return current_app.response_class(
        f"{dumps(data, indent=indent, separators=separators)}\n",
😲 Agile task management is now easier than calling a taxi. #Tracklify

Ways to fix

votes up 0 votes down

Arguments and keyword arguments should not be passed at the same time to jsonify.

How to reproduce:

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def index():
    return jsonify("text", i="test")

app.run()

Code with fix

from flask import Flask, jsonify

app = Flask(__name__)

@app.route("/")
def index():
    return jsonify(j="text", i="test") #<-- pass only arg or kwarg, not both

app.run()

May 27, 2021 cRyp70s answer
cRyp70s 113

Add a possible fix

Please authorize to post fix